Loading

LeetCode problem#283

Move Zeroes

Solved in 1 languageJava

Solve it on LeetCode(opens in a new tab)

Solution

16 lines
1class Solution {
2    public void moveZeroes(int[] nums) {
3        int j = 0;
4        for (int i = 0; i < nums.length; i++) {
5            if (nums[i] != 0) {
6                nums[j] = nums[i];
7                j++;
8            }
9        }
10
11        while (j < nums.length) {
12            nums[j] = 0;
13            j++;
14        }
15    }
16}

Want a walkthrough of this Java solution?

Get a step-by-step explanation of the idea behind the code.

All problems

Problem statements belong to LeetCode and are on leetcode.com. We're not affiliated with LeetCode.