双指针解法:
class Solution {
public:
void moveZeroes(vector<int>& nums) {
int i = 0,j = 0;
while(j < nums.size()){
if(nums[j] == 0){
j++;
}
else{
nums[i] = nums[j];
i++;
j++;
}
}
for(;i < nums.size();i++){
nums[i] = 0;
}
}
};
我的辣鸡算法:
class Solution {
public:
void moveZeroes(vector<int>& nums) {
int count = 0;
for(int i = 0;i < nums.size();i++){
if(nums[i] == 0){
nums.erase(nums.begin()+i);
count++;
i--;
}
}
for(int i = 0;i < count;i++){
nums.push_back(0);
}
}
};