给定一个非负整数数组 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. spring中的ResponseEntity理解

    参考: https://blog.csdn.net/weixin_37869477/article/details/82762976 https://blog.csdn.net/sswqzx/arti ...

  2. 03.Hibernate配置文件之核心配置文件

    一.核心配置文件的两种配置方式 1.属性文件方式 hibernate.properties(基本不会选用 hibernate.connection.driver_class=com.mysql.jdb ...

  3. vue 学习 一

    1.实例: var vm = new Vue({ el: '#example', data: { a:1 }, created: function () { // `this` 指向 vm 实例 co ...

  4. Joomla - akeeba backup(joomla网站备份、迁移扩展)

    在所有 joomla 的网站中,如果只允许安装一个扩展,估计超过90%的人都会选择 akeeba backup,这基本是每个joomla都必备的一个扩展: akeeba backup 的更多资料可以到 ...

  5. 装配SpringBean(二)--XML方式介绍

    装配SpringBean,我理解的意思就在容器中定义一个bean,然后让容器通过某种方式找到它.因为在Spring中一切皆资源,资源就是所谓的bean,我们怎么才能从容器中获取这些资源呢?那就是控制反 ...

  6. Hibernate通用Dao

    1. 接口 package com.coder163.main.dao; import org.hibernate.criterion.DetachedCriteria; import java.io ...

  7. spring-注解配置-junit整合测试-aop

    1 使用注解配置spring 1.1 步骤 导包4+2+spring-aop 1.为主配置文件引入新的命名空间(约束) 2.开启使用注解代理配置文件 3.在类中使用注解完成配置 1.2 将对象注册到容 ...

  8. Broken Keyboard UVA 11988 数组实现链表

    这个构造十分巧妙,,,又学到一招,有点类似数组实现的邻接表 #include <iostream> #include <string.h> #include <cstdi ...

  9. python twisted 的定时调用带参的函数

    无参情况:lc = task.LoopingCall(fun)如果fun带有参数,可以使用functools.partial传递 (fun2 = partial(fun, param1,[...]) ...

  10. Oracle ORA-01861

    Oracle 插入时间时 报错:ORA-01861: 文字与格式字符串不匹配 的解决办法 解决方法:   这个错误一般出现在时间字段上,即你插入的时间格式和数据库现有的时间格式不一致,解决的方法是格式 ...