Move Zeroes
Input: [0,1,0,3,12]
Output: [1,3,12,0,0]Solution
class Solution {
public void moveZeroes(int[] nums) {
// use two pointers strategy
// iterate through array
// create index to last position in array (this will indicate where to put 0 - or at least candidate where to put 0)
// if 0 is found -> move all left to right (if zero is found) - why not to go from right then?
// [true, false, true, false, false]
int start = 0;
int end = nums.length - 1;
while (start < end) {
int num1 = nums[start];
if (num1 == 0) {
int num2 = nums[end];
while (num2 == 0 && end > start) {
end--;
num2 = nums[end];
}
// swap values
if (num2 != 0) {
System.out.println(nums[start] + ":" + nums[end]);
int temp = nums[start];
nums[start] = num2;
nums[end] = temp;
}
}
start++;
}
}
}Last updated