一开始想用冒泡但是超时了,用了双指针:
class Solution { public void moveZeroes(int[] nums) { int i = 0,j = 0,n = nums.length; while(j<n){ while(i<n&&nums[i]!=0){ i++; } j = i; while(j<n&&nums[j]==0){ j++; } if(j>=n) break; swap(nums,i,j); } } private void swap(int[] nums,int i,int j){ int t = nums[i]; nums[i] = nums[j]; nums[j] = t; } }