给定一个非负整数数组 A,返回一个由 A 的所有偶数元素组成的数组,后面跟 A 的所有奇数元素。

 

SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。
class Solution {
public:
    vector<int> sortArrayByParity(vector<int>& A) {
        if(A.size()==0||A.size()==1)
            return A;
        vector<int>::iterator front = A.begin();
        vector<int>::iterator end = A.end() - 1;
        while(front < end)
        {
            while(front<end && *front%2 == 0)
                front ++;
            while(front<end && *end%2 == 1)
                end --;
            if(front < end)
            {
                auto temp = *front;
                *front = *end;
                *end = temp;
            }
        }
        return A;
        
    }
};

 按奇偶排序数组 随笔

 

扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄