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 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 <= 20000
A.length % 2 == 0
0 <= A[i] <= 1000
解题思想
- 使用 2 个 vector 分别保存奇数和偶数,最后依次在合起来。这个方法最直接。
vector<int> sortArrayByParityII(vector<int>& A) {
vector<int> odd;
vector<int> even;
for(auto it = A.begin(); it != A.end(); it++) {
// 偶数
if(*it % 2 == 0) {
even.push_back(*it);
}
// 奇数
else {
odd.push_back(*it);
}
}
int i = 0, j = 0, cnt = 0, len = A.size();
A.clear();
for(;cnt<len;cnt++) {
// 奇数位置
if(cnt%2 != 0) {
A.push_back(odd[i++]);
}
// 偶数位置
else {
A.push_back(even[j++]);
}
}
return A;
}
- 分别找偶数位置不是偶数,奇数位置不是奇数的地方进行互换。
vector<int> sortArrayByParityII(vector<int>& A) {
// 检查索引 i 和 A[i] 是否都是偶数
const auto checkeven = [&A](const int i)->bool {
return i%2==0 && A[i]%2==0;
};
// 检查索引 i 和 A[i] 是否都是奇数
const auto checkodd = [&A](const int i)->bool {
return i%2!=0 && A[i]%2!= 0;
};
// 找到索引 i 和A[i] 奇偶数不匹配的位置
const auto findIndex = [&A](int i, const auto func)->int {
while(i < A.size() && func(i) == true) {
i += 2;
}
return i;
};
int even = findIndex(0,checkeven);
int odd = findIndex(1, checkodd);
while(odd < A.size() && even < A.size()) {
swap(A[even], A[odd]);
even = findIndex(even, checkeven);
odd = findIndex(odd, checkodd);
}
return A;
}
LeetCode 922. Sort Array By Parity II C++ 解题报告的更多相关文章
- [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 ...
- #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. ...
- 992. Sort Array By Parity II - LeetCode
Question 992. Sort Array By Parity II Solution 题目大意:给一个int数组,一半是奇数一半是偶数,分别对偶数数和奇数数排序并要求这个数本身是偶数要放在偶数 ...
随机推荐
- Linux命令练习
1.开启Linux操作系统,要求以root用户登录GNOME图形界面,语言支持选择为汉语 2.使用快捷键切换到虚拟终端2,使用普通用户身份登录,查看系统提示符 3. 使用命令退出虚拟终端2上登录的用户 ...
- min_25筛题目总结
看了网上众多博客后,我才发现,实现min_25只有脑子,没有代码. 当然可能是我太ruo了. min_25是一种想法,不是算法. 不要尝试套模板,因为很多题目并没有什么用. 最重要的一点,g不要看成是 ...
- K2百家讲坛 | 越秀地产:K2为房企数字化转型带来更多可能
随着数字化经济时代的到来,房地产行业逐渐形成了新的竞争和市场格局,房企要在此背景下实现稳步发展,需要由原本的粗放式管理逐渐向集团性管理.精细化管控转变,这对房企的经营发展战略和业务管理方式都提出了不小 ...
- 'Tensorboard.util' has no attribute 'Retrier' - 'Tensorboard.util'没有属性'Retrier'
Here is a popular issue when you want to use tensorbard with your upgraded tensorflow and tensorboar ...
- 可视化布局html5
http://www.bootcss.com/p/layoutit/ http://layuiout.magicalcoder.com/magicaldrag-admin/drag
- Python2.0 与 3.0 的区别
Python 2.0 =默认编码=ASSIC=不支持中文 Python 3.0 =默认编码=UNICODE=默认支持中文 In summary : Python 2.x is legacy, Py ...
- 201671010142 2017-2 《java第十一章学习感悟》
事件处理基础 事件源,事件监听器,事件监听器 监听器接口的实现,监听器对象所属类必须实现与事件源相对应的接口,即必须提供接口中方法的实现. 适配器类 当程序用户试图关闭一个框架窗口时,Jframe对 ...
- getString与optString的区别
JSONObject.getString("key"):当对象中没有key属性的时候,会抛出No value for "key"的异常: public Stri ...
- 魅族pro 7详细打开Usb调试模式的方法
经常我们使用安卓手机链上Pc的时候,或者使用的有些APP比如我们公司营销小组经常使用的APP引号精灵,之前老版本就需要开启usb开发者调试模式下使用,现经常新版本不需要了,如果手机没有开启usb开发者 ...
- Linux:Linux Mint系统的安装
今天就更新一篇了,其实Linux系统大部分都是用虚拟机来安装的,毕竟Windows系统才是我们常用的系统,而Linux系统只是我们工作时才用的,而且使用虚拟机是非常方便的,不用重启电脑就可以使用另一种 ...