leetcode283 C++ 40ms 移除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++;
        }
    }
};
posted @ 2018-08-04 22:55  一条图图犬  阅读(104)  评论(0编辑  收藏  举报