leetcode——283——Move Zeroes

Given an array <code>nums</code>, write a function to move all <code>0</code>'s to the end of it while maintaining the relative order of the non-zero elements. <p>For example, given <code>nums  = [0, 1, 0, 3, 12]</code>, after calling your function, <code>nums</code> should be <code>[1, 3, 12, 0, 0]</code>. </p><p><strong>Note</strong>:
<ol><li>You must do this <strong>in-place</strong> without making a copy of the array.</li><li>Minimize the total number of operations.</li></ol></p><p></p>
class Solution {
public:
    void moveZeroes(vector<int>& nums) {
        int count = 0;
        for(int i = 0; i<nums.size();i++)
        {
            if(nums[i]==0)
               count++;
            else 
               nums[i-count] = nums[i];
        }
        for(int i = nums.size()-count;i<nums.size();i++)
           nums[i] = 0;
        
    }
};

你可能感兴趣的:(算法题)