Solution:
1. From left to right
class Solution { public void moveZeroes(int[] nums) { if (nums == null || nums.length < 2) { return; } int zeroIndex = 0, nonZeroIndex = 0, n = nums.length; while (nonZeroIndex < n) { while (zeroIndex < n && nums[zeroIndex] != 0) { zeroIndex++; } while (nonZeroIndex < n && nums[nonZeroIndex] == 0) { nonZeroIndex++; } if (nonZeroIndex == n || zeroIndex == n) { return; } if (zeroIndex < nonZeroIndex) { int temp = nums[zeroIndex]; nums[zeroIndex] = nums[nonZeroIndex]; nums[nonZeroIndex] = temp; } nonZeroIndex = zeroIndex + 1; } } }
class Solution { public void moveZeroes(int[] nums) { if (nums == null || nums.length < 2) { return; } int index = 0, n = nums.length; for (int i = 0; i < n; i++) { if (nums[i] != 0) { nums[index++] = nums[i]; } } while (index < n) { nums[index++] = 0; } } }