I have a question about the expected answer...


  • 0
    D

    Sorry this is my first time coding on leetcode, and "Move Zeroes" is my first test.
    I wrote down the process and the answer is correct, but when I submitted, it show as below:
    Input: [2,1]
    Output: [1,2]
    Expected: [2,1]

    Can anybody please tell me what is the problem? thanks very much!!!

    void moveZeroes(vector<int>& nums) {
        int mark=0;
        if (nums.empty()) {
            return;
        }
        for (int i=0; i<nums.size(); i++) {
            if (nums[i]!=0 && i!=0) {
                swap(nums[mark++],nums[i]);
            }
        }
    }

  • 0
    B

    Hmm referring to your code...

    for (int i=0; i<nums.size(); i++) {
        if (nums[i]!=0 && i!=0) {
            swap(nums[mark++],nums[i]);
        }
    }
    

    Have u tried ++mark instead? seems to me that the swap will occur before mark increments (post increment), causing swap (2,1) to be called when i=1. Correct me if i'm wrong.


Log in to reply
 

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