Leetcode922.Sort Array By Parity II按奇偶排序数组2
给定一个非负整数数组 A, A 中一半整数是奇数,一半整数是偶数。
对数组进行排序,以便当 A[i] 为奇数时,i 也是奇数;当 A[i] 为偶数时, i 也是偶数。
你可以返回任何满足上述条件的数组作为答案。
示例:
输入:[4,2,5,7] 输出:[4,5,2,7] 解释:[4,7,2,5],[2,5,4,7],[2,7,4,5] 也会被接受。
提示:
- 2 <= A.length <= 20000
- A.length % 2 == 0
- 0 <= A[i] <= 1000
class Solution {
public:
vector<int> sortArrayByParityII(vector<int>& A)
{
int len = A.size();
int cnt1 = len - 1;
int cnt2 = len - 1;
for(int i = 0; i < len; i++)
{
if((A[i] & 1) == 0 && (i & 1) == 1)
{
for(int j = cnt1; j > i; j--)
{
if((A[j] & 1) == 1 && (j & 1) == 0)
{
swap(A[i], A[j]);
cnt1 = j - 1;
break;
}
}
}
else if((A[i] & 1) == 1 && (i & 1) == 0)
{
for(int j = cnt2; j > i; j--)
{
if((A[j] & 1) == 0 && (j & 1) == 1)
{
swap(A[i], A[j]);
cnt2 = j - 1;
break;
}
}
}
}
return A;
}
};
Leetcode922.Sort Array By Parity II按奇偶排序数组2的更多相关文章
- [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 ...
- leetcode922 Sort Array By Parity II
""" Given an array A of non-negative integers, half of the integers in A are odd, and ...
- 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 ...
- 992. Sort Array By Parity II - LeetCode
Question 992. Sort Array By Parity II Solution 题目大意:给一个int数组,一半是奇数一半是偶数,分别对偶数数和奇数数排序并要求这个数本身是偶数要放在偶数 ...
- 【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 ...
- Sort Array By Parity II LT922
Given an array A of non-negative integers, half of the integers in A are odd, and half of the intege ...
- [Swift]LeetCode922.按奇偶排序数组 II | 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-按奇偶排序数组 II(Sort Array By Parity II)
这是悦乐书的第354次更新,第379篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第216题(顺位题号是922).给定非负整数的数组A,A中的一半整数是奇数,而剩下的一半 ...
随机推荐
- C/C++基础:main函数的参数argc和argv
转载: https://blog.csdn.net/Eastmount/article/details/20413773 一.main()函数参数 通常我们在写主函数时都是void main()或in ...
- /etc/sysctl.conf配置文件
# vi /etc/sysctl.conf # add by digoal.zhou fs.aio-max-nr = fs. kernel.core_pattern= /data01/corefile ...
- 修改linux命令行的提示符PS1
# If not running interactively, don't do anything [ -z "$PS1" ] && return # check ...
- Odoo Documentation : Recordsets
Other recordset operations Recordsets are iterable(可迭代的) so the usual Python tools are available for ...
- Creating a bootable Ubuntu USB stick
Windows: https://tutorials.ubuntu.com/tutorial/tutorial-create-a-usb-stick-on-windows#0 Ubuntu: http ...
- sudo apt-get update报错E: 部分索引文件下载失败。如果忽略它们,那将转而使用旧的索引文件。
解决方案1: 将对应的PPA删除掉即可 cd /etc/apt/suorces.list.d mv **.list **.list.bak 解决方案2: 更改源 cp /etc/apt/source_ ...
- light oj 1057 状压dp TSP
#include <iostream> #include <cstdlib> #include <cstring> #include <queue> # ...
- Jeecms之JSP访问action类
因为Jeecms采用spring容器管理类,在web容器加载的时候类都已经实例化好了.我们可以通过在JSP中访问spring上下文的方式来调用action业务类例: ApplicationContex ...
- mysql innodb 的 逻辑存储结构
如上图: innodb 的 逻辑存储单元分成 表空间,段,区,页 4个等级 默认情况下,一个数据库 所有变共享一个 默认的表空间(tablespan).可以指定每个表一个表空间. 一个表空间管理着 多 ...
- day65-test
目录 一.点击事件控制标签颜色 二.实现点击次数,变换页面标签的颜色 三.周期性实现颜色的旋转变色 练习题 一.点击事件控制标签颜色 1.有 红.黄.蓝 三个按钮,以及一个200x200矩形框box, ...