本文主要是介绍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.
For example, given nums = [0, 1, 0, 3, 12]
, after calling your function, nums
should be [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.
class Solution {
public:void moveZeroes(vector<int>& nums) {int size=nums.size();int i=0;vector<int>::iterator it=nums.begin();while(it!=nums.end()&&i<size){if(*it==0){it=nums.erase(it);nums.push_back(0);}else it++;i++;}}
};
这篇关于LeetCode *** 283. Move Zeroes的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!