I used two pointers to solve the problem.


  • 0
    O

    class Solution {
    public:
    void moveZeroes(vector<int>& nums) {
    int i = 0;
    int j = 0;
    while (i < nums.size()) {
    if (nums[j] == 0 && nums[i] != 0) {
    swap(nums[j], nums[i]);
    j++;
    }
    if (nums[j] != 0) j++;
    i++;
    }
    }
    };


Log in to reply
 

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