leetcode 905. 按奇偶排序数组 c++

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

你可以返回满足此条件的任何数组作为答案。

示例:

输入:[3,1,2,4]
输出:[2,4,3,1]
输出 [4,2,3,1],[2,4,1,3] 和 [4,2,1,3] 也会被接受。
class Solution {
public:
    vector<int> sortArrayByParity(vector<int>& A) {
        vector<int> xx;
        for(auto x:A){
            if(x%2 == 0)
                xx.push_back(x);
        }
        for(auto m:A){
            if(m%2 != 0)
                xx.push_back(m);
        }
        return xx;
    }
};

猜你喜欢

转载自blog.csdn.net/qq_23905237/article/details/86532167