Loading
Leetcode / 283. Move Zeroes

Pick a programming language:

Here is the source code for the solution to this problem.

class Solution {
    public void moveZeroes(int[] nums) {
        int j = 0;
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] != 0) {
                nums[j] = nums[i];
                j++;
            }
        }

        while (j < nums.length) {
            nums[j] = 0;
            j++;
        }
    }
}
Did you like the lesson? 😆👍
Consider a donation to support our work: