LeetCode-905. Sort Array By Parity

网友投稿 531 2022-08-25

LeetCode-905. Sort Array By Parity

LeetCode-905. 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 <= 5000​​​​0 <= A[i] <= 5000​​

题解:

class Solution {public: vector sortArrayByParity(vector& A) { int n = A.size(); vector odd, even; for (int i = 0; i < n; i++) { if (A[i] % 2 == 0) { even.push_back(A[i]); } else { odd.push_back(A[i]); } } even.insert(even.end(), odd.begin(), odd.end()); return even; }};

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:系统栈的工作原理(栈的实现原理)
下一篇:LeetCode-461. Hamming Distance
相关文章

 发表评论

暂时没有评论,来抢沙发吧~