Why My Code Got WA?


  • 0
    Y
    int count=n,j=0;
    for(int i=0;i<n;i++)
    {
        while(A[i]==elem)
    	{
    		count--;
            i++;
    	}
        if(i==n)
    		break;
    	A[j++]=A[i];
    }
    return count;
    

    When I rewrite it this way,I got AC! But I think the old one is right.

    int j=0;
            for(int i=0;i<n;i++)
            {
               if(A[i]!=elem)
                    A[j++]=A[i];
            }
            return j;

  • 0
    S
     while(A[i]==elem)
    {
        count--;
        i++;
        if(i==n)
        break;
    }
    

    it could get stuck inside the while


Log in to reply
 

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