[LeetCode] Remove Element


Given an array and a value, remove all instances of that value in place and return the new length.

The order of elements can be changed. It doesn't matter what you leave beyond the new length.

依然是双指针思想

 1 class Solution {
 2 public:
 3     int removeElement(int A[], int n, int elem) {
 4         // Start typing your C/C++ solution below
 5         // DO NOT write int main() function
 6         int start = 0;
 7         for(int i = 0; i < n; i++)
 8             if (elem != A[i])
 9             {
10                 A[start++] = A[i];
11             }
12             
13         return start;
14     }
15 };


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM