LeetCode - Remove Duplicates from Sorted Array

 

class Solution {
public:
    int removeDuplicates(int A[], int n) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
       
       if (n == 0)    return 0;

        int p=0;
        int q=1;

        while(q<n)
        {
            if(A[q]==A[p])
            {
                q++;
            }
            else
            {
                p++;
                A[p] = A[q];
                q++;
            }
        }
        
                
        return (p+1);
    }
};

 

 

Attention: edge case!!!

inputoutputexpected 
[] [0] []
 Wrong!

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

导航