
class Solution {
public void moveZeroes(int[] nums) {
int n = nums.length;
for(int left = 0,right = 0; right < n; right++){
if(nums[right] != 0){
int temp = nums[left];
nums[left++] = nums[right];
nums[right] = temp;
}
}
}
}重要思想: 通过双指针: 将数组划分为3个区间: [0,left-1] : 不为0元素 [left,right-1]:全为0元素 [right,n-1]: 待定元素

时间复杂度: O(n),n为数组元素的个数。 空间复杂度: O(1),算法只使用了常数级的额外空间(仅交换元素时使用的临时变量temp),因此空间复杂度为常数。