思路:
暴力法多次移动肯定是不行的,会超时。提出新的方法,通过快慢指针来实现
代码:
class Solution {
public:
void moveZeroes(vector<int>& nums) {
int fastIndex=0,slowIndex=0;
bool flag=true;
while(fastIndex<nums.size()){
if(flag==true){
if(nums[fastIndex]!=0){
fastIndex++;
slowIndex++;
continue;
}
fastIndex++;
flag=false;
}
else{
if(nums[fastIndex]!=0){
nums[slowIndex]=nums[fastIndex];
nums[fastIndex]=0;
slowIndex++;
}
fastIndex++;
}
}
}
};