给定一个非负整数数组 A,返回一个由 A 的所有偶数元素组成的数组,后面跟 A 的所有奇数元素。

你可以返回满足此条件的任何数组作为答案。

示例:

输入:[3,1,2,4] 输出:[2,4,3,1] 输出 [4,2,3,1],[2,4,1,3] 和 [4,2,1,3] 也会被接受。

提示:

  1. 1 <= A.length <= 5000
  2. 0 <= A[i] <= 5000

需要注意位运算的优先级,不加括号很可有结果不一样。最好加上括号

class Solution {
public:
vector<int> sortArrayByParity(vector<int>& A) {
int len = A.size();
int low = 0;
int high = len - 1;
while(low < high)
{
for(; low < high; low++)
{
if((A[low] & 1) == 1)
{
break;
}
}
for(; high > low; high--)
{
if((A[high] & 1) == 0)
{
break;
}
}
if(low < high)
{
swap(A[low], A[high]);
}
}
return A;
}
};

Leetcode905.Sort Array By Parity按奇偶排序数组的更多相关文章

  1. [LeetCode] 905. Sort Array By Parity 按奇偶排序数组

    Given an array A of non-negative integers, return an array consisting of all the even elements of A, ...

  2. LeetCode 905. Sort Array By Parity 按奇偶校验排列数组

    题目 Given an array A of non-negative integers, return an array consisting of all the even elements of ...

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

  4. 992. Sort Array By Parity II - LeetCode

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

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

  6. LeetCode 905. Sort Array By Parity

    905. Sort Array By Parity Given an array A of non-negative integers, return an array consisting of a ...

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

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

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

  9. 【LEETCODE】41、905. Sort Array By Parity

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

随机推荐

  1. HBase 物理视图

  2. 【一坨理论AC的题】Orz sxy大佬

    1.UVA10891 Game of Sum 2.LA4254 Processor . 3.UVA10905 Children's Game 4.UVA11389 The Bus Driver Pro ...

  3. 巧用 position:absolute

    1.跟随性 下面这种方法更加简便以及更方便维护, 例如“西部世界”,由于不用将父元素设为position:relative,position:absolute的位置也就不用根据文字多少而重新进行top ...

  4. win 7下安装mysql zip格式

    mysql 下载地址:https://dev.mysql.com/downloads/mysql/5.5.html#downloads 下载的mysql格式为zip: 下载完成放在除C盘以外的盘. 一 ...

  5. windows下 将tomcat做成服务,并于oracle后启动

    一.将tomcat做成服务 1.下载解压版的tomcat 6.*, 设置java.tomcat的环境(这个就不说了). 2.运行->cmd->到tomcat安装目录的bin目录: 3.运行 ...

  6. ROCR包中ROC曲线计算是取大于cutoff还是大于等于cutoff

    找到对应的代码如下 .compute.unnormalized.roc.curve function (predictions, labels) { pos.label <- levels(la ...

  7. 转:fork与vfork的区别

    源地址:http://blog.csdn.net/jianchi88/article/details/6985326 有大量驱动文章 fork()与vfock()都是创建一个进程,那他们有什么区别呢? ...

  8. Wsgi研究

    //转载自http://blog.kenshinx.me/blog/wsgi-research/ wsgi是一个搞web开发的pythoner必须了解的内容,之前也零散的看过一些文章,但总感觉好多概念 ...

  9. bootstrap-datetimepicker下ie8对indexOf的支持问题

    问题: 由于ie8不支持indexOf这个方法,所以在引入bootstrap-datetimepicker.js的时候js会抛出错误. 解决: // 在bootstrap-datetimepicker ...

  10. js作用域的销毁、不立即销毁、不销毁

    JavaScript中的函数执行会形成私有的作用域. (1)作用域的销毁 一般情况下,函数执行形成一个私有的作用域,当执行完成后就销毁了->节省内存空间 (2)作用域的不立即销毁 functio ...