problem
solution1:
class Solution { public: vector<int> sortArrayByParity(vector<int>& A) { vector<int> even, odd; for(auto a:A) { if(a%2==0) even.push_back(a); else odd.push_back(a); } even.insert(even.end(), odd.begin(), odd.end()); return even; } };
solution2:
class Solution { public: vector<int> sortArrayByParity(vector<int>& A) { for(int i=0, j=0; j<A.size(); j++) { if(A[j]%2==0) { swap(A[i++], A[j]);//i-even,j-odd; } } return A; } };
参考
1. Leetcode_easy_905. Sort Array By Parity;
2. discuss;
完