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. QQ音乐2019客户端-获取任意歌单完整歌曲列表和下载音乐文件方法

    步骤 1.在web网站上搜搜任意歌单 https://y.qq.com/#type=index/ 例如:中国好声音4.5.6.7.8季 打开后显示网址:  https://y.qq.com/n/yqq ...

  2. python-3-条件判断练习题

    前言 我们在前面两章学习了基础数据类型与条件判断语句,今天我们来做下练习题.如果你有不一样的解题思路在评论区亮出你的宝剑!!! 一.习题如下: 1.使用 while 循环输出 1 2 3 4 5 6 ...

  3. torch_11_风格迁移和cycleGAN

    1,A Neural Algorithm of atistic Style https://axiv.org/pdf/1508.06576.pdf 如何定义图片的内容,风格: 定义内容:在vggnet ...

  4. 解决原生javascript 缺少insertAfter的功能,非Jquery方法

    在现有的方法后插入一个新元素,你可能会想:既然有insertBefore方法,是不是也有一个相应的insertAfter()方法.很可惜,DOM没有提供方法.下面编写insertAfter函数,虽然D ...

  5. Vue.js 源码分析(二十五) 高级应用 插槽 详解

    我们定义一个组件的时候,可以在组件的某个节点内预留一个位置,当父组件调用该组件的时候可以指定该位置具体的内容,这就是插槽的用法,子组件模板可以通过slot标签(插槽)规定对应的内容放置在哪里,比如: ...

  6. 【Python】运算符

    python是强类型语言,某些场合下需要进行类型转换.关系运算符的结果是true或false.这里介绍一下基本的运算符,和几个使用实例,了解并掌握python中range()函数和math类库的引入和 ...

  7. 微信分享网页时自定义缩略图和简介(.net版本)

    要实现微信分享网页时自定义缩略图和简介,需开发者在公众平台网站中创建公众号.获取接口权限后,通过微信JS-SDK的分享接口,来实现微信分享功能. 下面来说明实现步骤. 第一部分 准备步骤 步骤一:注册 ...

  8. 使用三层架构+EF添加单元测试

    在运行测试的时候抛异常了: “System.InvalidOperationException”类型的异常在 mscorlib.dll 中发生,但未在用户代码中进行处理 The Entity Fram ...

  9. 消息服务dubbo接口性能压测性能优化案例

    最近项目中的消息服务做了运营商的改动,导致这个服务做了重新开发 压测脚本如下: 开启200线程压测: tps只有200-300之间,平均耗时在700ms左右 开启500线程压测 500并发压测,发现平 ...

  10. JavaScript调用百度地图

    在网站开发过程中,经常会调用到地图,百度地图提供Web开发.Android开发.iOS开发API及SDK,百度地图JavaScript API可帮助您在网站中构建功能丰富.交互性强的地图应用,本篇博客 ...