8 line 16ms c++ in-place solution


  • 0
    Y
    void moveZeroes(vector<int>& nums) {
        int sz = nums.size();
        int cnt = 0, tail = -1;
        for(int i=0; i<sz; i++){
            if(nums[i] == 0) continue;
            nums[++tail] = nums[i];
            cnt++;
        }
        for(int i=cnt; i<sz; i++) nums[i] = 0;
    }

Log in to reply
 

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