方法1 双指针、

代码1
public void moveZeroes(int[] nums) {int slow = 0;int fast =0 ;for(int i = 0 ; i < nums.length;i++){
while(fast< nums.length ){if(nums[fast] != 0){nums[slow] =nums[fast];slow++;}fast++;}while(slow<nums.length){nums[slow]=0;slow++;}}}
代码优化
public void moveZeroes(int[] nums) {int i =0;for(int j =0 ;j<nums.length;j++){if(nums[j]!= 0 ){int temp = nums[j];nums[j]= nums[i];nums[i]=temp;i++;}}}
方法二 把 nums 当作栈
public void moveZeroes(int[] nums) {int stackSize = 0; for(int x : nums){if(x != 0){nums[stackSize++] =x;}}Arrays.fill(nums,stackSize,nums.length,0);}