给定一个非负整数数组 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] 也会被接受。

提示:

  1. 2 <= A.length <= 20000
  2. A.length % 2 == 0
  3. 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的更多相关文章

  1. [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 ...

  2. leetcode922 Sort Array By Parity II

    """ Given an array A of non-negative integers, half of the integers in A are odd, and ...

  3. 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 ...

  4. 992. Sort Array By Parity II - LeetCode

    Question 992. Sort Array By Parity II Solution 题目大意:给一个int数组,一半是奇数一半是偶数,分别对偶数数和奇数数排序并要求这个数本身是偶数要放在偶数 ...

  5. 【LEETCODE】42、922. Sort Array By Parity II

    package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * ...

  6. 【Leetcode_easy】922. Sort Array By Parity II

    problem 922. Sort Array By Parity II solution1: class Solution { public: vector<int> sortArray ...

  7. 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 ...

  8. [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 ...

  9. LeetCode.922-按奇偶排序数组 II(Sort Array By Parity II)

    这是悦乐书的第354次更新,第379篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第216题(顺位题号是922).给定非负整数的数组A,A中的一半整数是奇数,而剩下的一半 ...

随机推荐

  1. Numpy数据的操作 * dot() multiply() 的区别

    使用numpy时,跟matlab不同: 1.* dot() multiply() 对于array来说,* 和 dot()运算不同 *是每个元素对应相乘 dot()是矩阵乘法 对于matrix来说,*  ...

  2. 廖雪峰Java11多线程编程-2线程同步-1同步代码块

    1.线程安全问题 多个线程同时运行,线程调度由操作系统决定,程序本身无法决定 如果多个线程同时读写共享变量,就可能出现问题 class AddThread extends Thread{ public ...

  3. html常用标签详解4-列表标签

    列表标签 列表标签分为3大类:无序列表.有序列表.自定义列表.线面我会依依简单介绍 一.无序列表 有个type属性: 默认:disc:实心小圆点:circle:空心小圆点:square:实心小方块:n ...

  4. SQLServer:目录

    ylbtech-SQLServer:目录 1.返回顶部   2. 文档返回顶部 · https://docs.microsoft.com/zh-cn/sql/sql-server/sql-server ...

  5. 在Bat批处理中调用Powershell脚本

    ##如何在BAT中调用powershell,把下面代码另存为bat格式pushd %~dp0powershell.exe -command ^  "& {set-executionp ...

  6. <form>(表单)标签和常用的类型

    1.定义和用法 <form> 标签用于为用户输入创建 HTML 表单. 表单能够包含 input 元素,比如文本字段.复选框.单选框.提交按钮等等. 表单还可以包含 menus.texta ...

  7. PAT甲级——A1038 Recover the Smallest Number

    Given a collection of number segments, you are supposed to recover the smallest number from them. Fo ...

  8. require模块开发(一)

    1.require下载和加载 1.1 下载 工欲善其事必先利其器,先下载require.js下载地址, 然后添加 require.js 到 scripts 目录 1.2 加载 然后加载require ...

  9. 移植别人的vcpkg包到自己的项目

    修改该目录下的文件即可: 或者修改你的项目文件下的所有不对的路径,类似于这种:

  10. Visual Studio 2013打开Orchard1.8.1版本源码,出现无法打开解决方法

    当使用Visual Studio打开项目时,如果遇到如上情况,可能是缓存问题,删除解决方案缓存(suo后缀文件)后重新打开sln解决方案即可,如下图