@Test public void moveZeroes() { int[] nums = {0, 1, 0, 3, 12}; int[] result = {1, 3, 12, 0, 0}; MoveZeroesSolution.moveZeroes(nums); for (int i = 0; i < nums.length; i++) { assertEquals(nums[i], result[i]); } }
static void moveZeroes(int[] nums) { int len; if (nums == null || (len = nums.length) <= 1) return; boolean doSomething = false; int index; int indexFront; for (int i = 1; i < len; i++) { index = nums[len - i]; indexFront = nums[len - i - 1]; if (indexFront == 0 && index != 0) { nums[len - i] = indexFront; nums[len - i - 1] = index; doSomething = true; } } if (!doSomething) return; moveZeroes(nums); }
MoveZeroesSolution { static void moveZeroes(int[] nums) { int len; if (nums == null || (len = nums.length) <= 1) return; boolean doSomething = false; int index; int indexFront; for (int i = 1; i < len; i++) { index = nums[len - i]; indexFront = nums[len - i - 1]; if (indexFront == 0 && index != 0) { nums[len - i] = indexFront; nums[len - i - 1] = index; doSomething = true; } } if (!doSomething) return; moveZeroes(nums); } }
MoveZeroesSolution { static void moveZeroes(int[] nums) { int len; if (nums == null || (len = nums.length) <= 1) return; boolean doSomething = false; int index; int indexFront; for (int i = 1; i < len; i++) { index = nums[len - i]; indexFront = nums[len - i - 1]; if (indexFront == 0 && index != 0) { nums[len - i] = indexFront; nums[len - i - 1] = index; doSomething = true; } } if (!doSomething) return; moveZeroes(nums); }  }
MoveZeroesSolution { static void moveZeroes(int[] nums) { int len; if (nums == null || (len = nums.length) <= 1) return; boolean doSomething = false; int index; int indexFront; for (int i = 1; i < len; i++) { index = nums[len - i]; indexFront = nums[len - i - 1]; if (indexFront == 0 && index != 0) { nums[len - i] = indexFront; nums[len - i - 1] = index; doSomething = true; } } if (!doSomething) return; moveZeroes(nums); }   }
MoveZeroesSolution { static void moveZeroes(int[] nums) { int len; if (nums == null || (len = nums.length) <= 1) return; boolean doSomething = false; int index; int indexFront; for (int i = 1; i < len; i++) { index = nums[len - i]; indexFront = nums[len - i - 1]; if (indexFront == 0 && index != 0) { nums[len - i] = indexFront; nums[len - i - 1] = index; doSomething = true; } } if (!doSomething) return; moveZeroes(nums); }    }