remove duplicates from sorted array

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

 

posted on 2013-09-03 22:46  jumping_grass  阅读(151)  评论(0)    收藏  举报

导航