Sunday, December 30, 2012

[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.
» Solve this problem

[解题思路]
双指针。

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


No comments: