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 * ...
随机推荐
- 理解JWT
1. JSON Web Token是什么 JSON Web Token (JWT)是一个开放标准(RFC 7519),它定义了一种紧凑的.自包含的方式,用于作为JSON对象在各方之间安全地传输信息.该 ...
- iotop实时监控磁盘io
介绍 Linux下的IO统计工具如iostat, nmon等大多数是只能统计到per设备的读写情况, 如果你想知道每个进程是如何使用IO的就比较麻烦. iotop 是一个用来监视磁盘 I/O 使用状况 ...
- pin, port, cell, net
一幅图即可解释清楚: 更标准的官方解释:
- leyou_06_Nginx的自启
1.在linux系统的/etc/init.d/目录下创建nginx文件 vim /etc/init.d/nginx 添加以下内容 #!/bin/sh # # nginx - this script s ...
- (转载)关于My97 datepicker与Angular ng-model绑定问题解决。
转载自 http://zerosoft.blog.51cto.com/679447/1611403 <input type="text" ng-model="d&q ...
- 交叉熵-loss-理解
参考链接: https://blog.csdn.net/tsyccnh/article/details/79163834
- JasperReports报表区段14
我们将在本章开始,一个简单的报表模板的结构看.依样画葫芦JasperReports的结构报表模板归类到多个区段.部分是有规定的高度,并且可以包含像直线,矩形,图像或文本字段对象报表的部分. 通过提供的 ...
- Python中函数的定义必须在调用的前面
# -*- coding:utf-8 -*- Python中函数的定义必须在调用的前面,但是在函数的内部调用一个函数,不用考虑顺序,只要被调用的函数被定义了即可 #标准的先函数定义,后函数调用def ...
- zabbix告警模板
邮件 webhook模板 ZABBIX告警通知 告警状态:[{TRIGGER.STATUS}] 告警主机:[{HOST.NAME}] 主机地址:[{HOST.IP}] 告警时间:[{EVENT.DAT ...
- 2019阿里云开年Hi购季满返活动火热报名中!
摘要: 在每年开年的这个大幅度优惠促销月,怎样才能花最少的钱配置最特惠的云服务?请看本文! 2019阿里云云上采购季活动已经于2月25日正式开启,从已开放的活动页面来看,活动分为三个阶段: 2月25日 ...