LeetCode.905-按奇偶排序数组(Sort Array By Parity)
这是悦乐书的第347次更新,第371篇原创
01 看题和准备
今天介绍的是LeetCode算法题中Easy级别的第212题(顺位题号是905)。给定一个非负整数数组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
02 第一种解法
将数组A中的偶数元素存到List中,奇数元素存到List2中,创建一个新的数组result,长度和A相等,先遍历List,将偶数元素存入result的前面,再遍历List2,将奇数元素跟在偶数元素后面。
此解法的时间复杂度是O(N),空间复杂度是O(N)。
public int[] sortArrayByParity(int[] A) {
List<Integer> list = new ArrayList<Integer>();
List<Integer> list2 = new ArrayList<Integer>();
int n = A.length;
for (int i=0; i<n; i++) {
if (A[i]%2 == 0 ) {
list.add(A[i]);
} else {
list2.add(A[i]);
}
}
int[] result = new int[n];
int index = 0;
for (int j=0; j<list.size(); j++) {
result[index++] = list.get(j);
}
for (int j=0; j<list2.size(); j++) {
result[index++] = list2.get(j);
}
return result;
}
03 第二种解法
针对第一种解法,我们再简化下,不使用List存数据,我们分两次遍历处理A中的元素,第一次只要偶数元素,存入新数组result中,第二次只要奇数元素,存入新数组result中。
此解法的时间复杂度是O(N),空间复杂度是O(N)。
public int[] sortArrayByParity2(int[] A) {
int n = A.length, index = 0;
int[] result = new int[n];
for (int i=0; i<n; i++) {
if (A[i]%2 == 0 ) {
result[index++] = A[i];
}
}
for (int j=0; j<n; j++) {
if (A[j]%2 != 0 ) {
result[index++] = A[j];
}
}
return result;
}
04 第三种解法
我们也可以只使用一个循环,不使用额外的数组,借助双指针来解题。
创建两个指针,一个从A的第一位开始,记为i,另外一个从A的最后一位开始,记为j。如果i对应的元素为奇数,且j对应的元素是偶数,那么就需要将两个元素互换,互换后,偶数排在了前面,奇数换到了后面。另外我们还需要让两个指针移动,以便遍历完所有元素,如果i对应的元素为偶数,就跳到下一个元素,同理,如果j对应的元素为奇数,就跳到前一个元素,直到i不小于j。
此解法的时间复杂度是O(N),空间复杂度是O(1)。
public int[] sortArrayByParity3(int[] A) {
int i = 0, j = A.length-1;
while (i < j) {
if (A[i]%2 !=0 && A[j]%2 ==0) {
int tem = A[j];
A[j] = A[i];
A[i] = tem;
}
if (A[i]%2 ==0) {
i++;
}
if (A[j]%2 !=0) {
j--;
}
}
return A;
}
05 小结
算法专题目前已连续日更超过六个月,算法题文章215+篇,公众号对话框回复【数据结构与算法】、【算法】、【数据结构】中的任一关键词,获取系列文章合集。
以上就是全部内容,如果大家有什么好的解法思路、建议或者其他问题,可以下方留言交流,点赞、留言、转发就是对我最大的回报和支持!
LeetCode.905-按奇偶排序数组(Sort Array By Parity)的更多相关文章
- [Swift]LeetCode905. 按奇偶排序数组 | 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. 按奇偶排序数组
给定一个非负整数数组 A,返回一个由 A 的所有偶数元素组成的数组,后面跟 A 的所有奇数元素. 你可以返回满足此条件的任何数组作为答案. 示例: 输入:[3,1,2,4] 输出:[2,4,3,1] ...
- 力扣(LeetCode)按奇偶排序数组II 个人题解
给定一个非负整数数组 A, A 中一半整数是奇数,一半整数是偶数. 对数组进行排序,以便当 A[i] 为奇数时,i 也是奇数:当 A[i] 为偶数时, i 也是偶数. 你可以返回任何满足上述条件的数组 ...
- [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 ...
- LeetCode905.按奇偶排序数组
905.按奇偶排序数组 问题描述 给定一个非负整数数组 A,返回一个由 A 的所有偶数元素组成的数组,后面跟 A 的所有奇数元素. 你可以返回满足此条件的任何数组作为答案. 示例 输入:[3,1,2, ...
- 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 ...
- 905. Sort Array By Parity - LeetCode
Question 905. Sort Array By Parity Solution 题目大意:数组排序,偶数放前,奇数在后,偶数的数之间不用管顺序,奇数的数之间也不用管顺序 思路:建两个list, ...
- 【LEETCODE】41、905. Sort Array By Parity
package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * ...
- 992. Sort Array By Parity II - LeetCode
Question 992. Sort Array By Parity II Solution 题目大意:给一个int数组,一半是奇数一半是偶数,分别对偶数数和奇数数排序并要求这个数本身是偶数要放在偶数 ...
随机推荐
- 九度OJ 1141:Financial Management (财务管理) (平均数)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:939 解决:489 题目描述: Larry graduated this year and finally has a job. He's ...
- grouped differently across partitions
[熵增] 由无序到有序 http://spark.apache.org/docs/latest/rdd-programming-guide.html#shuffle-operations Shuffl ...
- 编写灵活、稳定、高质量的 HTML 和 CSS 代码的规范。
引用地址http://codeguide.bootcss.com/#html-ie-compatibility-mode <!DOCTYPE html> <html lang=& ...
- react-native填坑--react-navigation
Navigator已经被React Native废弃了.也许你可以在另外的一个依赖库里react-native-deprecated-custom-components里找到.不过既然官方推荐的是re ...
- 关于MySQL的information_schema库简单介绍及实际应用
本文简介 写本文主要是围绕下面几点进行的. 1.information_schema数据库到底是做什么用的? 2.执行alter table 表名 modify column 字段名 类型 这个sql ...
- MySQL登陆及配置
一.mysql用户登录 mysql –u用户名 [–h主机名或者IP地址] –p密码 说明:用户名是你登录的用 户,主机名或者IP地址为可选项,如果是本地连接则不需要,远程连接需要填写,密码是对应用户 ...
- Objective-C学习之解析XML
通过soap请求webservice时,返回的数据是XML类型,有时候也需要解析本地的xml数据等,苹果自带类NSXMLParser解析xml还是很方便的,简单轻便 本文以解析本地XML为例,网络获取 ...
- jquery回顾part1——选择器
jQuery 选择器 选择器 实例 选取 * $("*") 所有元素 #id $("#lastname") id="lastname" 的元 ...
- php排序方法之快速排序
$arr = array(3,55,45,2,67,76,6.7,-65,85,4); function quickSort($arr){ if (count($arr) <= 1){ retu ...
- codeforces 701B B. Cells Not Under Attack(水题)
题目链接: B. Cells Not Under Attack 题意: n*n的棋盘,现在放m个棋子,放一个棋子这一行和这一列就不会under attack了,每次放棋子回答有多少点还可能under ...