It's strange that AC when added with a useless judgement but WA without it.


  • 0
    W

    It's strange that my code should "Wrong answer" without such line, as when the program have not returned yet it has to meet the requirement of "first < s.size()" but OJ give me WA.
    But when I add this "useless" IF it become AC....

    The idea of this algorithm is quite simple but why it WA if we remove the "useless" IF below?

    class Solution {
    public:
    void moveZeroes(vector<int>& nums) {
    int p=nums.size();
    int first=0;
    int number=0;
    while(nums[first]!=0) { first++;}
    //cout<<"first is "<<first<<" and size is"<<nums.size()<<endl;
    if(first==nums.size() ) return ; //no zeros

            number=1;//at least one zero
        for(int i=first+1;i<nums.size();i++)
        {
            if(nums[i]!=0)
            { int tmp=nums[i];
                 nums[i]=nums[i-number];
                 nums[i-number]=tmp;
            }
            else
            {
                number++;
            }
        }
        ***if(first<nums.size() )  // if you delete this line it will Wrong Answer***
        { for(int i=nums.size()-number;i<nums.size();i++)
           nums[i]=0;
        }
    }
    

    };


Log in to reply
 

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