#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 <= A.length <= 50000 <= 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 五题打卡结束 下班班了

浙公网安备 33010602011771号