【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;

posted on 2019-07-26 19:21  鹅要长大  阅读(77)  评论(0编辑  收藏  举报

导航