[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 };
posted @ 2012-11-14 15:32  chkkch  阅读(4423)  评论(0编辑  收藏  举报