LeetCode 283. Move Zeroes
For example, given nums = [0, 1, 0, 3, 12], after calling your function, nums should be [1, 3, 12, 0, 0].
https://leetcode.com/problems/move-zeroes/
class Solution {
public:
void moveZeroes(vector<int>& nums) {
vector<int> v;
int count_of_zero=0;
for(int i=0;i<nums.size();i++){
if(nums[i] != 0){
v.push_back(nums[i]);
}else{
count_of_zero++;
}
}
int idx=-1;
for(int i=0;i<v.size();i++){
idx++;
cout << v[i] << endl;
nums[i]=v[i];
}
for(int i=idx+1;i< nums.size() ;i++){
nums[i]=0;
}
}
};
Ref:
https://www.tutorialspoint.com/cpp_standard_library/vector.htm
留言
張貼留言