这是悦乐书的第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)的更多相关文章

  1. [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, ...

  2. 力扣(LeetCode) 905. 按奇偶排序数组

    给定一个非负整数数组 A,返回一个由 A 的所有偶数元素组成的数组,后面跟 A 的所有奇数元素. 你可以返回满足此条件的任何数组作为答案. 示例: 输入:[3,1,2,4] 输出:[2,4,3,1] ...

  3. 力扣(LeetCode)按奇偶排序数组II 个人题解

    给定一个非负整数数组 A, A 中一半整数是奇数,一半整数是偶数. 对数组进行排序,以便当 A[i] 为奇数时,i 也是奇数:当 A[i] 为偶数时, i 也是偶数. 你可以返回任何满足上述条件的数组 ...

  4. [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 ...

  5. LeetCode905.按奇偶排序数组

    905.按奇偶排序数组 问题描述 给定一个非负整数数组 A,返回一个由 A 的所有偶数元素组成的数组,后面跟 A 的所有奇数元素. 你可以返回满足此条件的任何数组作为答案. 示例 输入:[3,1,2, ...

  6. 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 ...

  7. 905. Sort Array By Parity - LeetCode

    Question 905. Sort Array By Parity Solution 题目大意:数组排序,偶数放前,奇数在后,偶数的数之间不用管顺序,奇数的数之间也不用管顺序 思路:建两个list, ...

  8. 【LEETCODE】41、905. Sort Array By Parity

    package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * ...

  9. 992. Sort Array By Parity II - LeetCode

    Question 992. Sort Array By Parity II Solution 题目大意:给一个int数组,一半是奇数一半是偶数,分别对偶数数和奇数数排序并要求这个数本身是偶数要放在偶数 ...

随机推荐

  1. [通信]Linux User层和Kernel层常用的通信方式

    转自:https://bbs.csdn.net/topics/390991551?page=1 netlink:https://blog.csdn.net/stone8761/article/deta ...

  2. 关于4Ps 、4Cs 、4Rs 、4Vs营销策略的内容及优劣比较

  3. leetcode leetcode 783. Minimum Distance Between BST Nodes

    Given a Binary Search Tree (BST) with the root node root, return the minimum difference between the ...

  4. html5--3.17 新增的表单重写

    html5--3.17 新增的表单重写 学习要点 对form元素的属性做一个小结,对个别属性进行一点补充 重点掌握新增的表单重写 form元素的属性小结 action/method/enctype/n ...

  5. Object.prototype.constructor

    Returns a reference to the Object function that created the instance's prototype. 注意这个属性的值是函数本省的引用,而 ...

  6. Ubuntu 16.04上安装SkyEye及测试

    说明一下,在Ubuntu 16.04上安装SkyEye方法不是原创,是来自互联网,仅供学习参考. 一.检查支持软件包 gcc,make,vim(optional),ssh,subversionbinu ...

  7. 非旋treap套线段树

    BZOJ3065. 去年用pascal 块链过了.. 今年来试了试非旋treap大法   注定被块链完爆 代码留这. 第一份 :辣鸡的  垃圾回收做法  跑得极慢 #include <bits/ ...

  8. POJ1904(有向图缩点+输入输出挂参考)

    King's Quest Time Limit: 15000MS   Memory Limit: 65536K Total Submissions: 8311   Accepted: 3017 Cas ...

  9. 蓝桥杯 2014本科C++ B组 地宫取宝 DFS+记忆化搜索

    历届试题 地宫取宝   时间限制:1.0s   内存限制:256.0MB 问题描述 X 国王有一个地宫宝库.是 n x m 个格子的矩阵.每个格子放一件宝贝.每个宝贝贴着价值标签. 地宫的入口在左上角 ...

  10. hibernate VS mybatis

    1: 一般来说,业务逻辑比较简单,集增删改查就可以满足需求,建议使用hibernate,而复杂的业务逻辑,尤其是多表关联查询,建议使用mybatis. 2: hibernate有更好的二级缓存机制,可 ...