码迷,mamicode.com
首页 > 编程语言 > 详细

leetcode283 C++ 40ms 移除0

时间:2018-08-05 00:32:11      阅读:150      评论:0      收藏:0      [点我收藏+]

标签:code   amp   tor   ||   leetcode   fast   int   public   ast   

class Solution {
public:
    void moveZeroes(vector<int>& nums) {
        if(nums.empty() || nums.size() == 1){
            return;
        }
        auto slow = nums.begin();
        auto fast = slow +1;
        int temp;
        while(slow < fast && fast != nums.end()){
            while(*slow!=0){
                slow++;
                if(slow == nums.end()){
                    return;
                }
            }
            fast = slow + 1;
            if(fast == nums.end()){
                return;
            }
            while(*fast == 0){
                fast++;
                if(fast == nums.end()){
                    return;
                }
            }
            temp = *slow;
            *slow = *fast;
            *fast = temp;
            slow++;
            fast++;
        }
    }
};

leetcode283 C++ 40ms 移除0

标签:code   amp   tor   ||   leetcode   fast   int   public   ast   

原文地址:https://www.cnblogs.com/theodoric008/p/9420505.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!