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中的一半整数是奇数,而剩下的一半 ...
随机推荐
- mybatis和hibernate的特点
第一方面:开发速度的对比 就开发速度而言,Hibernate的真正掌握要比Mybatis来得难些.Mybatis框架相对简单很容易上手,但也相对简陋些.个人觉得要用好Mybatis还是首先要先理解好H ...
- WINDOWS选择目录SHBrowseForFolder使用方法介绍
首先介绍一个兼容Unicode和多字节的方法,定义如下头文件: // TString.h; #pragma once #include <string> #ifdef UNICODE ty ...
- html常用标签6-表单标签
1.表单的初始化标签 <form action="#" method="get"><!--表单的开始--> </form> ...
- thinkPHP使用中踩的坑,记录一下(不停更)
版本3.2.3 1.数据库操作中的连贯操作table(),在查询的时候可以切换表,但是在插入,更新的时候请不要使用.例如 D('user')->table('auth')->add($da ...
- js 设置缓存
长期存储 localStorage.getItem("key"); //获取键的值 localStorage.setItem("key" ...
- Hdu 2376
题目链接 题意:给出一颗含有n个结点的树,树上每条边都有一个长度,求树上所有路径的平均长度. 考虑树上每条边对所有路径长度和的贡献,对于每条偶 就是边的两个短点u和v,只需要记录以u为根的子树的结点的 ...
- Vue.之.回到顶部
Vue.之.回到顶部 当页面出现上下滚动条时,页面右下角出现回到顶部功能. 在页面上添加如下DIV(写的CSS内部样式),这个DIV功能:出现滚动条往下滑动,就显示出来,反之隐藏.点击DIV快速回到顶 ...
- LUOGU P3539 [POI2012]ROZ-Fibonacci Representation
传送门 解题思路 打了个表发现每次x只会被比x大的第一个fab或比x小的第一个fab表示,就直接写了个爆搜骗分,结果过了.. 代码 #include<iostream> #include& ...
- agc004E Salvage Robots
题意: 一个网格图,有若干机器人,还有一个出口. 操作一系列指令让机器人一起上下左右走,走出矩形就死,进入出口则得救. 最多救多少机器人? $W,H \leq 100$ 考虑不让所有机器人移动,而让出 ...
- 组合数学起步-排列计数[ZJOI2010][BZOJ2111]
<题面> 数据范围:$1 \leq N \leq 10^6, P \leq 10^9 $ 这个题…… 以为是排列,其实是组合 题目中说是从所有排列中找到Magic的,就是 $p_{i/2} ...