leetcode-283-Move-Zeroes

Given an array nums, write a function to move all 0’s to the end of it while maintaining the relative order of the non-zero elements.

Example:

1
2
Input: [0,1,0,3,12]
Output: [1,3,12,0,0]

Note:

You must do this in-place without making a copy of the array.
Minimize the total number of operations.

我的解决思路如下:
便利向量nums, 统计有多少0值,将0值数删除,最后补0

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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);
i--;
count++;
}
}

for(int i = 0; i < count; i++)
{
nums.push_back(0);
}
}
};