leetcode刷题,总结,记录,备忘 283

来源:互联网 发布:网络评选投票 编辑:程序博客网 时间:2024/06/11 14:25

leetcode283Move 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:

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

Credits:
Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.

leetcode新题出的好快,,,我这速度估计永远刷不完。

这题是道简单题,就不多做解释了。

class Solution {public:    void moveZeroes(vector<int>& nums) {                int count = 0;                for (vector<int>::iterator it = nums.begin(); it != nums.end();)        {            if (*it == 0)            {                nums.erase(it);                count++;            }            else            {                ++it;            }        }                for (int i = 0; i < count; ++i)        {            nums.push_back(0);        }    }};


0 0
原创粉丝点击