C++ straightforward code


  • 0
    B
    void moveZeroes(vector<int>& nums) {
        int left = 0, right = 1; 
        while (right<nums.size())
          {
              if (nums[left] == 0 && nums[right] == 0) { right++; continue;}    //consecutive zeros
              if (nums[left] == 0) { swap(nums[left],nums[right]); left++; right++; continue; }
              left++; right++;  //both non-zero
          }
        
    }

Log in to reply
 

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