首页 > 编程语言 > 详细

leetcode283 C++ 40ms 移除0

时间:2018-08-04 23:46:41      阅读:195      评论:0      收藏:0      [点我收藏+]
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

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!