Given an array of nums, write a function to move all zeros to the end of the array while maintaining the relative order of non-zero elements.
Example:
Input: [0,1,0,3,12]
Output: [1,3,12,0,0]
Explanation: You
must operate on the original array, you cannot copy additional arrays.
Minimize the number of operations.
Source: LeetCode
Link: https://leetcode-cn.com/problems/move-zeroes
Method: pointer
public static void moveZeroes(int[] nums) {
int j = 0;
for (int i = 0; i < nums.length; i++) {
if (nums[i] != 0) {
nums[j++] = nums[i];
}
}
while (j < nums.length) {
nums[j++] = 0;
}
}