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, followed by all the odd elements of A.
You may return any answer array that satisfies this condition.
Example 1:
Input: [3,1,2,4]
Output: [2,4,3,1]
The outputs [4,2,3,1], [2,4,1,3], and [4,2,1,3] would also be accepted.
Note:
- 1 <= A.length <= 5000
- 0 <= A[i] <= 5000
题解
题目大意:数组排序,偶数放前,奇数在后,偶数的数之间不用管顺序,奇数的数之间也不用管顺序
思路:建两个list,一个放偶数,一个放奇数,最后将两个list合并,转化为数组返回
class Solution {
public int[] sortArrayByParity(int[] A) {
ArrayList<Integer> oddList = new ArrayList<>();//存放奇数
ArrayList<Integer> evenList = new ArrayList<>();//存放偶数
for (int a : A) {
if (a % 2 == 0) evenList.add(a);
else oddList.add(a);
}
evenList.addAll(oddList);
int[] arr = new int[A.length];
for (int i = 0; i < A.length; i ++){
arr[i] = evenList.get(i);
}
return arr;
}
}
LeetCode 905. 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
905. Sort Array By Parity Given an array A of non-negative integers, return an array consisting 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 ...
- [leetcode] 905. Sort Array By Parity [easy]
原题链接 很水的一道题,就是数组内部交换. 水题就想着减少复杂度嘛,于是学到一种交换写法. class Solution { public: vector<int> sortArrayBy ...
- 【LEETCODE】41、905. Sort Array By Parity
package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * ...
- 905. Sort Array By Parity - LeetCode
Question 905. Sort Array By Parity Solution 题目大意:数组排序,偶数放前,奇数在后,偶数的数之间不用管顺序,奇数的数之间也不用管顺序 思路:建两个list, ...
- 【Leetcode_easy】905. Sort Array By Parity
problem 905. Sort Array By Parity solution1: class Solution { public: vector<int> sortArrayByP ...
- [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 ...
- 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 ...
随机推荐
- GO基础之函数
一.Go语言函数的格式 函数构成了代码执行的逻辑结构,在Go语言中,函数的基本组成为:关键字 func.函数名.参数列表.返回值.函数体和返回语句,每一个程序都包含很多的函数,函数是基本的代码块. 函 ...
- JS 中 判断数据类型 typeof详解
typeof 可用来获取检测变量的数据类型 语法 typeof operand typeof(operand) 参数 operand 一个表示对象或原始值的表达式,其类型将被返回. 描述 下表总结 ...
- js延时定时器
// 获取图片方向延时器 getImageOrientationTimer(context) { if (context.imageTimeout) return; if (context.image ...
- RS422接线 z-tek RS232 TO RS485/RS422
接线方式 z-tek 引脚定义
- RSA 非对称加密算法的Java实现
关于RSA的介绍Google一下很多,这里不做说明.项目开发中一般会把公钥放在本地进行加密,服务端通过私钥进行解密.Android项目开发中要用到这个加密算法,总结后实现如下: import andr ...
- iOS 常用算法之设计一个算法,验证某字符是否为合法IPV4字符
浅析 : 一个IPV4字符由3个大于0小于255的数字 以及 3个点构成, 所有我们需要判断小数点数量是否满足条件, 以及小数点隔开的每部分是否满足条件即可. 思路: 1. 校验是否有3个小数点; 2 ...
- [20190910]索引分支块中TERM使用什么字符表示.txt
[20190910]索引分支块中TERM使用什么字符表示.txt --//做索引块转储,一些root,分支节点出现TERM,从来没有关注使用字符表示,简单探究看看. 1.环境:SCOTT@test01 ...
- 微信小程序之 catalog 切换
组件名称:catalog 组件属性:catalogData,type:String 组件描述:这是一个子组件,数据从父组件中传递 效果图: catalog 目录为多个,使用 scroll-view 容 ...
- OpenTelemetry项目中的Observability
最近,在实操zipkin,jaeger,opencensus,opentracing,opentelemetry等. opentelemetry将Observability提到了重要页面, 并进行了讲 ...
- 2. Linux文件与目录管理
一.目录与路径 1. 相对路径与绝对路径 绝对路径:路径写法[一定由根目录 / 写起],如:/usr/share/doc 相对路径:路径写法[不由 / 写起], /usr/share/doc 要到 / ...