A simple java solution using queue


  • 0
    Y
    public class Solution {
        public void moveZeroes(int[] nums) {
            Queue<Integer> zero = new LinkedList<>();
            for (int i = 0; i < nums.length; i++) {
                if (nums[i] == 0) {
                    zero.offer(i);
                    continue;
                }
                if (!zero.isEmpty()) {
                    nums[zero.poll()] = nums[i];
                    nums[i--] = 0;
                }
            }
        }
    }

Log in to reply
 

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