题意
就是把一些列数组中的0给弄到最后去
思路
对数组中遍历,非0添加到新数组,用下标标记,到遍历结束,把下标之后的数组元素都填0
(一看代码就懂 = =)
代码
class Solution {
public:
void moveZeroes(vector<int>& nums) {
int len = nums.size();
int idx = 0;
for(int i=0;i<len;i++){
if(nums[i]){
nums[idx++] =nums[i];
}
}
for(int i=idx;i<len;i++){
nums[i] = 0;
}
}
};
happy coding
seen