给定一个非负整数数组 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. mac上开启22号端口

    在苹果maC系统SSH 远程登录服务器,采用默认22端口,登录出现了以下的提示: @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ ...

  2. AppServer获取参数的方法

    AppServer中从APP_PARAM表中根据param_code获取param_value: appManageService.getParamValueByCode(param_code) -- ...

  3. myeclipse 配置svn

    方法三:直接解压下载SVN插件:site-1.6.10.zip解压后将其全部文件拷贝至:D:\Program Files\Genuitec\MyEclipse 8.5\dropins(MyEclips ...

  4. 在skyline中将井盖、雨水箅子等部件放到地面模型上

    公司三维建模组遇到这样的一个问题,怎样将井盖.雨水盖子恰好放在做好的地面模型上.传统的方法是在skyline中逐个调整井盖的对地高度,就是调整为恰好能放在地面上.或者选择很粗糙的一个方法,在“高度”属 ...

  5. MyBatis配置文件(三)--typeAliases别名

    因为类的全限定名一般包括包名,显得很长,在使用过程中不是很方便,所以MyBatis中允许我们使用一种简写的方式来代替全限定名,这就是别名.这就相当于我们在玩微信的时候,有些人的昵称很长很难记,怎么办? ...

  6. Django-rest Framework(六)

    不懂使用机制的直接看源码就好了,也不是很难,能够看得懂 视图家族 1. View:将请求方式与视图类的同名方法建立映射,完成请求响应(原生django) from django.views impor ...

  7. Merge array and hash in ruby if key appears in array

    I have two arrays one = [1,2,3,4,5,6,7] and two = [{1=>'10'},{3=>'22'},{7=>'40'}] Two will ...

  8. Springboot2集成Activiti设计器并去除security

    前言 鉴于项目需要将acitiviti设计器整合到原工程中,在网上查了不少资料都不太适用,经过借鉴和自己倒腾终于搞定了,分享一下经验,如果有问题,可以在留言区咨询. 文中用到的资源代码链接: http ...

  9. MyEclipse6.5安装SVN插件方法

    MyEclipse6.5安装SVN插件,掌握了几种方法,本节就像大家介绍一下MyEclipse6.5安装SVN插件的三种方法,看完本文你肯定有不少收获,希望本文能教会你更多东西. 一.安装方法: My ...

  10. Vue-cli3.x中使用Axios发送跨域请求的配置方法

    Vue-cli3.x中使用Axios发送跨域请求的配置方法 安装axios npm i axios -s main.js中引入 import axios from 'axios' //将axios挂载 ...