LeetCode- odd and even sorted array

Subject description:

Given a non-negative integer array A, returns an array in the array, all even after element A along all odd elements.

You can return any array satisfies this condition as the answer.

Example:

Input: [3,1,2,4]
Output: [2,4,3,1]
Output [4,2,3,1], [2,4,1,3] and [4,2,1, 3] will be accepted.

class Solution {
public:
    vector<int> sortArrayByParity(vector<int>& A) {
        vector<int> v = A;
        int left = 0,right = A.size()-1;
        int i = 0;
        while(left < right)
        {
            while(i < A.size())
            {
                if(A[i] % 2 == 0)
                {
                    v[left] = A[i];
                    ++left;
                }
                else
                {
                    v[right] = A[i];
                    --right;
                }
                 ++i;
            }
        }
        return v;
    }
};

 

Published 119 original articles · won praise 17 · views 20000 +

Guess you like

Origin blog.csdn.net/tangya3158613488/article/details/104066164