问题描述(原题链接)
给定一个数组 nums,编写一个函数将所有 0 移动到数组的末尾,同时保持非零元素的相对顺序。
请注意 ,必须在不复制数组的情况下原地对数组进行操作。
代码:
class Solution {
public void moveZeroes(int[] nums) {
//双指针
int len = nums.length;
int left=0,right=0;
while(right<len){
if(nums[right]!=0){
int temp = nums[left];
nums[left]=nums[right];
nums[right] = temp;
left++;
}
right++;
}
}
}