按奇偶排序数组
给定一个非负整数数组 A,返回一个由 A 的所有偶数元素组成的数组,后面跟 A 的所有奇数元素。
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; } };

浙公网安备 33010602011771号