Was wrong with my solution?


  • 0
    X
    class Solution {
    public:
        int removeDuplicates(int A[], int n) {
    		int now=0;
    		for(int i=0;i<n;i++){
    			if(i!=0 && A[i]==A[now]) continue;
    			A[now++]=A[i];
    		}
            
    		return now;
        }
    };
    

    I got Output Limit Exceeded,but i got right answer on my PC.


  • 0
    L

    maybe you can make a little change

    for(int i=0;i<n-1;i++){
                if(i!=0 && A[i]==A[i+1]) continue;
                A[now++]=A[i];
            }
    

    and here is my code:

    class Solution {
    public:
        int removeDuplicates(int A[], int n) {
            int i,j=1;
            if(n<2) return n;
            
            for(i=0;i<n-1;i++)
            {
                if(A[i]!=A[i+1]) 
                A[j++]=A[i+1];
            }
            return j;
        }
    };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.