记录一个下标,对原数组进行遍历,当非0时,将该数置于该下标内,并将下标值+1
最终从下标到数组尾全部置0.
/**
* @author johnsondu
* @problem Move Zeroes
* @url https://leetcode.com/problems/move-zeroes/
* @timeComlexity O(n)
* @spaceComplexity O(1)
* @strategy See code.
*/
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;
}
};