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> even, odd;
for (int num : A) {
if (num % 2 == 0) even.push_back(num);
else odd.push_back(num);
}
even.insert(even.end(), odd.begin(), odd.end());
return even;
}
};

我们也可以优化空间复杂度,不新建额外的数组,而是采用直接交换数字的位置,使用两个指针i和j,初始化均为0。然后j往后遍历,若遇到了偶数,则将 A[j] 和 A[i] 交换位置,同时i自增1,这样操作下来,同样可以将所有的偶数都放在奇数前面,参见代码如下:


解法二:

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

我们还可以使用 STL 的内置函数 partition,是专门用来给数组重新排序的,不过我们要重写排序方式,将偶数的都放在前面即可,参见代码如下:


解法三:

class Solution {
public:
vector<int> sortArrayByParity(vector<int>& A) {
partition(A.begin(), A.end(), [](auto a) { return a % 2 == 0; });
return A;
}
};

Github 同步地址:

https://github.com/grandyang/leetcode/issues/905

参考资料:

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

https://leetcode.com/problems/sort-array-by-parity/discuss/170734/C%2B%2BJava-In-Place-Swap

https://leetcode.com/problems/sort-array-by-parity/discuss/170725/Know-your-C%2B%2B-Algorithms!-This-is-std%3A%3Apartition-%3A)

[LeetCode All in One 题目讲解汇总(持续更新中...)](https://www.cnblogs.com/grandyang/p/4606334.html)

[LeetCode] 905. Sort Array By Parity 按奇偶排序数组的更多相关文章

  1. LeetCode 905. Sort Array By Parity 按奇偶校验排列数组

    题目 Given an array A of non-negative integers, return an array consisting of all the even elements of ...

  2. Leetcode905.Sort Array By Parity按奇偶排序数组

    给定一个非负整数数组 A,返回一个由 A 的所有偶数元素组成的数组,后面跟 A 的所有奇数元素. 你可以返回满足此条件的任何数组作为答案. 示例: 输入:[3,1,2,4] 输出:[2,4,3,1] ...

  3. LeetCode 905. Sort Array By Parity

    905. Sort Array By Parity Given an array A of non-negative integers, return an array consisting of a ...

  4. LeetCode 905 Sort Array By Parity 解题报告

    题目要求 Given an array A of non-negative integers, return an array consisting of all the even elements ...

  5. [leetcode] 905. Sort Array By Parity [easy]

    原题链接 很水的一道题,就是数组内部交换. 水题就想着减少复杂度嘛,于是学到一种交换写法. class Solution { public: vector<int> sortArrayBy ...

  6. 【LEETCODE】41、905. Sort Array By Parity

    package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * ...

  7. 905. Sort Array By Parity - LeetCode

    Question 905. Sort Array By Parity Solution 题目大意:数组排序,偶数放前,奇数在后,偶数的数之间不用管顺序,奇数的数之间也不用管顺序 思路:建两个list, ...

  8. 【Leetcode_easy】905. Sort Array By Parity

    problem 905. Sort Array By Parity solution1: class Solution { public: vector<int> sortArrayByP ...

  9. [LeetCode] 922. Sort Array By Parity II 按奇偶排序数组之二

    Given an array A of non-negative integers, half of the integers in A are odd, and half of the intege ...

随机推荐

  1. 深圳龙华有轨电车BIM项目

    本项目是“龙华有轨电车BIM+GIS运维管理平台“研发组成的内容之一,包含站台.电车.变电所等模型绘制. 龙华区有轨电车是深圳市的一条位于龙华区的有轨电车线路,项目规划了三条线路,总长51公里.试验线 ...

  2. linux中dd命令详解

    本文转自:https://www.cnblogs.com/yuanqiangfei/p/9138625.html 一.dd命令的解释 dd:用指定大小的块拷贝一个文件,并在拷贝的同时进行指定的转换. ...

  3. Vue.js 源码分析(二十七) 高级应用 异步组件 详解

    当我们的项目足够大,使用的组件就会很多,此时如果一次性加载所有的组件是比较花费时间的.一开始就把所有的组件都加载是没必要的一笔开销,此时可以用异步组件来优化一下. 异步组件简单的说就是只有等到在页面里 ...

  4. 安利一波ubuntu18.04作为开发环境,极度舒适

    乌班图18更新也一年多了吧,除了最开始的尝鲜,最近才真正使用起来.用完的感受是完爆Windows,比起OSX也不差. 开发环境需要的东西: git shell idea chrome firefox ...

  5. 一些 Java 和 Android 的参考资料

    1. .net程序员转战android第三篇---登录模块之静态登录 2. .net程序员转战android第二篇---牛刀小试 3. .net程序员转战android第一篇---环境部署 4. 一些 ...

  6. 安装Redis(Windows版)

    1,GitHub下载地址:https://github.com/MicrosoftArchive/redis/tags 2,进行安装(一直下一步即可) 注:我这里安装的地址是 D:Redis 3,在电 ...

  7. Java学习——注解

    Java学习——注解 摘要:本文主要介绍了Java开发常用的注解,以及如何自定义注解. 部分内容来自以下博客: https://www.cnblogs.com/Qian123/p/5256084.ht ...

  8. 面试阿里百分百问的Jvm,别问有没有必要学,真的很有必要朋友

    面试阿里百分百问的Jvm,别问有没有必要学,真的很有必要朋友 前言: JVM 的内存模型和 JVM 的垃圾回收机制一直是 Java 业内从业者绕不开的话题(实际调优.面试)JVM是java中很重要的一 ...

  9. IOC : Unity 配置和使用

    原文出自:IOC : Unity 配置和使用 之前Terry Lee 已经介绍过Unity的简单使用了,不过那篇文章是针对旧版本的,现在的版本1.2版略有不同. 我下载了Unity并做了一个简单的测试 ...

  10. JavaWeb项目 IDEA+Tomcat+Nginx 部署流程

    转载请注明原文地址:https://www.cnblogs.com/ygj0930/p/11375100.html 一:IDEA Maven项目打包 1.修改打包方式 在maven项目的pom文件中, ...