Leetcode905.Sort Array By Parity按奇偶排序数组
给定一个非负整数数组 A,返回一个由 A 的所有偶数元素组成的数组,后面跟 A 的所有奇数元素。
你可以返回满足此条件的任何数组作为答案。
示例:
输入:[3,1,2,4] 输出:[2,4,3,1] 输出 [4,2,3,1],[2,4,1,3] 和 [4,2,1,3] 也会被接受。
提示:
- 1 <= A.length <= 5000
- 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按奇偶排序数组的更多相关文章
- [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, ...
- LeetCode 905. Sort Array By Parity 按奇偶校验排列数组
题目 Given an array A of non-negative integers, return an array consisting of all the even elements of ...
- [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 ...
- 992. Sort Array By Parity II - LeetCode
Question 992. Sort Array By Parity II Solution 题目大意:给一个int数组,一半是奇数一半是偶数,分别对偶数数和奇数数排序并要求这个数本身是偶数要放在偶数 ...
- 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 ...
- 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 ...
- 【LEETCODE】42、922. Sort Array By Parity II
package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * ...
- 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 ...
- 【LEETCODE】41、905. Sort Array By Parity
package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * ...
随机推荐
- 2019-5-24-WPF-源代码-从零开始写一个-UI-框架
title author date CreateTime categories WPF 源代码 从零开始写一个 UI 框架 lindexi 2019-05-24 15:54:36 +0800 2018 ...
- pyinstaller 打包python3.6文件成exe 运行
1.安装pyinstaller 切换到安装目录下script 运行 如我的目录:F:\Program Files\Python36\Scripts pip install pyinstaller ...
- 【python之路46】内置函数2,是【python之路18】的补充
将3.5版本中的68个内置函数,按顺序逐个进行了自认为详细的解析.为了方便记忆,将这些内置函数进行了如下分类: 数学运算(7个) 类型转换(24个) 序列操作(8个) 对象操作(7个) 反射操作(8个 ...
- 吴恩达《机器学习》课程总结(5)_logistic回归
Q1分类问题 回归问题的输出可能是很大的数,而在分类问题中,比如二分类,希望输出的值是0或1,如何将回归输出的值转换成分类的输出0,1成为关键.注意logistics回归又称 逻辑回归,但他是分类问题 ...
- jeecms框架单点登录功能的实现
单点登录的功能实现主要原理: 1: 在点击登录按钮的时候使用reponse.addCookie()方法向浏览器发送cookie: 2: 在前段拦截器中的request.getCookie()在接收到设 ...
- LRU Cache数据结构简介
什么是LRU Cache LRU是Least Recently Used的缩写,意思是最近最少使用,它是一种Cache替换算法. 什么是Cache?狭义的Cache指的是位于CPU和主存间的快速RAM ...
- redis学习笔记04-事务
1.redis事务 事务实际上指的是一组命令的集合,执行时会按顺序串行的执行,中途不能加入其它命令.它用来解决批处理需求. 在redis中的基本使用如下: >multi ok >incr ...
- 常见Idea插件
一.Maven Helper Maven Helper用来查找和排除Jar包冲突的依赖关系. 安装: 打开Idea的Settings→Plugins→在输入框中输入“maven helper”→点击I ...
- 跟我一起做一个vue的小项目(二)
这个vue项目是紧跟着之前的项目跟我一起做一个vue的小项目(一)来的. 我继续后面的开发(写的比较粗糙,边学边记录) 下图是header头部的样式 header组件内容如下 //header.vue ...
- 【python之路40】Python 作用域
python的作用域与javaScript是一样的,参考:http://www.cnblogs.com/sunshuhai/p/9112578.html 一.python是以函数作为作用域的 if 1 ...