#Leetcode# 905. Sort Array By Parity

https://leetcode.com/problems/sort-array-by-parity/

 

Given an array A of non-negative integers, return an array consisting of all the even elements of A, followed by all the odd elements of A.

You may return any answer array that satisfies this condition.

 

Example 1:

Input: [3,1,2,4]
Output: [2,4,3,1]
The outputs [4,2,3,1], [2,4,1,3], and [4,2,1,3] would also be accepted.

 

Note:

  1. 1 <= A.length <= 5000
  2. 0 <= A[i] <= 5000

代码:

class Solution {
public:
    vector<int> sortArrayByParity(vector<int>& A) {
        vector<int> ans;
        int n = A.size();
        for(int i = 0; i < n; i ++)
            if(!(A[i] % 2)) ans.push_back(A[i]);
        for(int i = 0; i < n; i ++) 
            if(A[i] % 2) ans.push_back(A[i]);
        
        return ans;
    }
};

  今天 Leetcode 五题打卡结束 下班班了

posted @ 2019-04-10 21:36  丧心病狂工科女  阅读(123)  评论(0编辑  收藏  举报