【Leetcode_easy】922. Sort Array By Parity II

problem

922. Sort Array By Parity II

solution1:

class Solution {
public:
    vector<int> sortArrayByParityII(vector<int>& A) {
        int n = A.size();
        for(int i=0, j=1; i<n, j<n; )
        {
            while(i<n && A[i]%2==0) i += 2;
            while(j<n && A[j]%2==1) j += 2;
            if(j<n) swap(A[i], A[j]);
        }
        return A;
    }
};

参考

1. Leetcode_easy_922. Sort Array By Parity II;

2. discuss;

原文地址:https://www.cnblogs.com/happyamyhope/p/11252325.html