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 * ...
随机推荐
- hbuilder scss自动编译
hbuilder 命令参数: --no-cache %FileName% %FileBaseName%.css --style compact
- 局部内部类为什么只能访问final局部变量,对于成员变量却可以随便访问?
局部内部类为什么只能访问final局部变量,对于成员变量却可以随便访问? public class OuterClass { private int memberField = 10; public ...
- light oj 1105 规律
#include <iostream> #include <algorithm> #include <cstring> #include <cstdio> ...
- DFS-深度优先搜索与BFS-广度优先搜索
1.DFS DFS是一个递归过程.(类似于二叉树的前序遍历) 参考:深度优先搜索(Depth-First-Search)精髓 2.BFS 可以理解为按层遍历,借助队列结构来实现.(类似于二叉树的层次遍 ...
- etcd 研究研究
先记录参考信息:etcd 场景https://blog.csdn.net/bbwangj/article/details/82584988 etcd 集群部署https://www.jianshu.c ...
- springboot 2 Hikari 多数据源配置问题(dataSourceClassName or jdbcUrl is required)
springboot 2 Hikari 多数据源配置问题(dataSourceClassName or jdbcUrl is required) 最近在项目中想试一下使用 Hikari 连接池,以前用 ...
- Win3.2有人用过么
老牌子的操作系统. 了,有人用过么,我还在用,感觉挺不错的,哈哈哈哈哈. 1.0那一代,画面粗糙,几乎不能算GUI操作系统. 2.0,画面较1.0一代明显改善,可惜的是,手头没货. 3.0 ...
- matlab中disp函数的简单用法
输出数组类型的数据,也可以把string类型的数据看做数组输出 输出数字 >> num = ; >> disp(num) 输出字符串 >> disp('this i ...
- join和os.path.join 的用法
Python中有join和os.path.join()两个函数,具体作用如下: join:连接字符串数组.将字符串.元组.列表中的元素以指定的字符(分隔符)连接生成一个新的字符串os.path.joi ...
- Luogu P3254 圆桌问题(最大流)
P3254 圆桌问题 题面 题目描述 假设有来自 \(m\) 个不同单位的代表参加一次国际会议.每个单位的代表数分别为 \(r_i (i =1,2,--,m)\) . 会议餐厅共有 \(n\) 张餐桌 ...