LeetCode - Remove Element

Easy!

O(n)

 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         
 7         int p=0;
 8         int q=0;
 9         while(q<n)
10         {
11             if(A[q]==elem)
12             {
13                q++;
14             }
15             else
16             {
17                 A[p] = A[q];
18                 p++; q++;
19             }
20         }
21         
22         return p;
23     }
24 };

 

posted on 2013-05-04 09:11  highstar88  阅读(137)  评论(0编辑  收藏  举报

导航