[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 <= 50000 <= 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
[LeetCode All in One 题目讲解汇总(持续更新中...)](https://www.cnblogs.com/grandyang/p/4606334.html)
[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 ...
- Leetcode905.Sort Array By Parity按奇偶排序数组
给定一个非负整数数组 A,返回一个由 A 的所有偶数元素组成的数组,后面跟 A 的所有奇数元素. 你可以返回满足此条件的任何数组作为答案. 示例: 输入:[3,1,2,4] 输出:[2,4,3,1] ...
- 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 ...
- LeetCode 905 Sort Array By Parity 解题报告
题目要求 Given an array A of non-negative integers, return an array consisting of all the even elements ...
- [leetcode] 905. Sort Array By Parity [easy]
原题链接 很水的一道题,就是数组内部交换. 水题就想着减少复杂度嘛,于是学到一种交换写法. class Solution { public: vector<int> sortArrayBy ...
- 【LEETCODE】41、905. Sort Array By Parity
package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * ...
- 905. Sort Array By Parity - LeetCode
Question 905. Sort Array By Parity Solution 题目大意:数组排序,偶数放前,奇数在后,偶数的数之间不用管顺序,奇数的数之间也不用管顺序 思路:建两个list, ...
- 【Leetcode_easy】905. Sort Array By Parity
problem 905. Sort Array By Parity solution1: class Solution { public: vector<int> sortArrayByP ...
- [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 ...
随机推荐
- Debug 路漫漫-15:Python: NameError:name 'dataset' is not defined
在调试 <Outer Product-based Neural Collaborative Filtering>论文的源码(https://github.com/duxy-me/ConvN ...
- 一、Spring注解之@ComponentScan
Spring注解之@ComponentScan [1]@ComponentScan注解是什么 @ComponentScan主要就是定义扫描的路径从中找出标识了需要装配的类自动装配到spring的bea ...
- 十、自定义ThreadPoolExecutor线程池
自定义ThreadPoolExecutor线程池 自定义线程池需要遵循的规则 [1]线程池大小的设置 1.计算密集型: 顾名思义就是应用需要非常多的CPU计算资源,在多核CPU时代,我们要让每一个CP ...
- switch的对象不能为null
我写的NPE 虽然不多, 但几乎每次系统出问题的时候,看到api返回值是空的,绝大多数是NPE造成的. 这时候会感慨一下谁写的bug,然后去补判空代码. 最近抽风,开始给自己写的代码添加UnitTes ...
- CSS的基础学习
CSS学习 --------学习资源 http://www.csszengarden.com/ CSS语法检查http://jigsaw.w3.org/css-validator/ 配置CSS的方法: ...
- 一个简单的利用 WebClient 异步下载的示例(二)
继上一篇 一个简单的利用 WebClient 异步下载的示例(一) 后,我想把核心的处理提取出来,成 SkyWebClient,如下: 1. SkyWebClient 该构造函数中 downloadC ...
- matplotlib的使用——pie(饼图)的使用
在我们进行数据分析的时候需要对得出的数据进行可视化,因此我们需要引入第三方包来帮助我们进行可视化分析,在这里使用matplotlib 一.安装 使用指令[pip install matplotlib] ...
- VMware+windbg时快照功能的使用
VMware+windbg时快照功能的使用 一.快照功能的使用 我们在编写与调试驱动文件时经常会面临死机蓝屏情况,如果像平常一样重启,再重新配置环境,肯定会占用大量时间. Vmware虚拟机给我们提供 ...
- Grafana的Docker部署方式
docker run -d -p : --name=grafana544 -v D:/grafana/grafana-/data:/var/lib/grafana -v D:/grafana/graf ...
- WPF 有任何Focus问题,就找 Keyboard_Navigation
牛牛的博客 https://www.cnblogs.com/smallidea/p/3412699.html 参照官网教程: https://docs.microsoft.com/en-us/dotn ...