[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 integers are even.
Sort the array so that whenever A[i] is odd, i is odd; and whenever A[i] is even, i is even.
You may return any answer array that satisfies this condition.
Example 1:
Input: [4,2,5,7]
Output: [4,5,2,7]
Explanation: [4,7,2,5], [2,5,4,7], [2,7,4,5] would also have been accepted.
Note:
2 <= A.length <= 20000A.length % 2 == 00 <= A[i] <= 1000
这道题是之前那道 [Sort Array By Parity](https://www.cnblogs.com/grandyang/p/11173513.html) 的拓展,那道让把奇数排在偶数的后面,而这道题是让把偶数都放在偶数坐标位置,而把奇数都放在奇数坐标位置。博主最先想到的方法非常简单粗暴,直接分别将奇数和偶数提取出来,存到两个不同的数组中,然后再把两个数组,每次取一个放到结果 res 中即可,参见代码如下:
解法一:
class Solution {
public:
vector<int> sortArrayByParityII(vector<int>& A) {
vector<int> res, even, odd;
for (int num : A) {
if (num % 2 == 0) even.push_back(num);
else odd.push_back(num);
}
for (int i = 0; i < even.size(); ++i) {
res.push_back(even[i]);
res.push_back(odd[i]);
}
return res;
}
};
论坛上还有一种更加简单的方法,不需要使用额外的空间,思路是用两个指针,i指针一直指向偶数位置,j指针一直指向奇数位置,当 A[i] 是偶数时,则跳到下一个偶数位置,直到i指向一个偶数位置上的奇数,同理,当 A[j] 是奇数时,则跳到下一个奇数位置,直到j指向一个奇数位置上的偶数,当 A[i] 和 A[j] 分别是奇数和偶数的时候,则交换两个数字的位置,从而满足题意,参见代码如下:
解法二:
class Solution {
public:
vector<int> sortArrayByParityII(vector<int>& A) {
int n = A.size(), i = 0, j = 1;
while (i < n && j < n) {
if (A[i] % 2 == 0) i += 2;
else if (A[j] % 2 == 1) j += 2;
else swap(A[i], A[j]);
}
return A;
}
};
Github 同步地址:
https://github.com/grandyang/leetcode/issues/922
类似题目:
参考资料:
https://leetcode.com/problems/sort-array-by-parity-ii/
[LeetCode All in One 题目讲解汇总(持续更新中...)](https://www.cnblogs.com/grandyang/p/4606334.html)
[LeetCode] 922. Sort Array By Parity II 按奇偶排序数组之二的更多相关文章
- Leetcode922.Sort Array By Parity II按奇偶排序数组2
给定一个非负整数数组 A, A 中一半整数是奇数,一半整数是偶数. 对数组进行排序,以便当 A[i] 为奇数时,i 也是奇数:当 A[i] 为偶数时, i 也是偶数. 你可以返回任何满足上述条件的数组 ...
- LeetCode 922. Sort Array By Parity II C++ 解题报告
922. Sort Array By Parity II 题目描述 Given an array A of non-negative integers, half of the integers in ...
- #Leetcode# 922. Sort Array By Parity II
https://leetcode.com/problems/sort-array-by-parity-ii/ Given an array A of non-negative integers, ha ...
- 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 i ...
- 【LEETCODE】42、922. Sort Array By Parity II
package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * ...
- 【Leetcode_easy】922. Sort Array By Parity II
problem 922. Sort Array By Parity II solution1: class Solution { public: vector<int> sortArray ...
- 【LeetCode】922. Sort Array By Parity II 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 使用奇偶数组 排序 奇偶数位置变量 日期 题目地址: ...
- 【leetcode】922. Sort Array By Parity II
题目如下: 解题思路:非常简单的题目,引入两个变量oddInx = 1和evenInx = 0,和与A等长的结果数组res.然后遍历A,如果A[i]为偶数,则令res[evenInx] = A[i], ...
- 【leetocde】922. Sort Array By Parity II
Given an array of integers nums, half of the integers in nums are odd, and the other half are even. ...
随机推荐
- centos7 更换为aliyun的yum源
rm -f /etc/yum.repos.d/* wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Ce ...
- [KMP]一本通(http://ybt.ssoier.cn:8088) 1698:字符串匹配
字符串匹配 [题目描述] 对于一个字符集大小为C的字符串pp,可以将任意两个字符在p中的位置进行互换,例如p=12321,交换1.21.2得到21312,交换1.4得到42324,交换可以进行任意次. ...
- JVM的监控工具之jstack
参考博客:https://www.jianshu.com/p/213710fb9e40 jstack(Stack Trace for Java)命令用于生成虚拟机当前时刻的线程快照(一般称为threa ...
- docker安装kafka
文本摘自此文章 .kafka需要zookeeper管理,所以需要先安装zookeeper. 下载zookeeper镜像 $ docker pull wurstmeister/zookeeper .启动 ...
- netCore3.0+webapi到前端vue(后端)
第一步创建api项目 创建完成启动F5!! 如图 数据库我用的是mysql 用ef操作数据 开发环境:Win10 + VS2019Mysql服务器版本:8.0.16 1.下载并安装插件(必备) MyS ...
- 利用Chrome开发者工具功能进行网页整页截图的方法
第一步:打开chrome开发者工具. 打开你想截图的网页,然后按下 F12(macOS 是 option + command + i)调出开发者工具,接着按「Ctrl + Shift + P」(mac ...
- django中视图函数中装饰器
方法一 给指定方法加 from django.utils.decorators import method_decorator class xx(View): @method_decorator(装饰 ...
- PHP 类/对象函数
PHP类/对象函数是PHP核心的一部分,无需要安装就可以使用. 函数名称 描述 __autoload 尝试加载未定义的类 class_alias 为一个类创建别名 class_exists 检查类是否 ...
- BUUCTF Hack World
有返回 ,基于布尔得盲注这里用到异或注入(个人喜欢这样用)1^0 返回 Hello, glzjin wants a girlfriend.1^1 返回 Error Occured When Fetch ...
- 关于使用Hadoop MR的Eclipse插件开发时遇到Permission denied问题的解决办法【转】
搭建了一个Hadoop的环境,Hadoop集群环境部署在几个Linux服务器上,现在想使用windows上的Java客户端来操作集群中的HDFS文件,但是在客户端运行时出现了如下的认证错误,被折磨了几 ...