这是悦乐书的第354次更新,第379篇原创

01 看题和准备

今天介绍的是LeetCode算法题中Easy级别的第216题(顺位题号是922)。给定非负整数的数组A,A中的一半整数是奇数,而剩下的一半是偶数。

对数组进行排序,以便每当A[i]为奇数时,i就是奇数; A[i]是偶数,i就是偶数。

你可以返回满足此条件的任何答案数组。例如:

输入:[4,2,5,7]

产出:[4,5,2,7]

说明:[4,7,2,5],[2,5,4,7],[2,7,4,5]也将被接受。

注意

  • 2 <= A.length <= 20000

  • A.length%2 == 0

  • 0 <= A [i] <= 1000

02 第一种解法

使用两个List将奇数、偶数分别存起来,创建一个新的数组result,如果索引为奇数,就从存奇数的List中取值作为新数组的元素,反之就从存偶数的List中取值作为新数组的元素。

此解法的时间复杂度是O(N),空间复杂度是O(N)

public int[] sortArrayByParityII(int[] A) {
List<Integer> odd = new ArrayList<Integer>();
List<Integer> even = new ArrayList<Integer>();
for (int num : A) {
if (num%2 == 0) {
even.add(num);
} else {
odd.add(num);
}
}
int j = 0, k = 0;
int[] result = new int[A.length];
for (int i=0; i<result.length; i++) {
if (i%2 == 0) {
result[i] = even.get(j++);
} else {
result[i] = odd.get(k++);
}
}
return result;
}

03 第二种解法

我们也可以直接从A中取值,同样是新建一个result数组,对result数组新建两个索引,一个从0开始,只做偶数索引,另一个从1开始,只做奇数索引,分两次遍历A数组,将对应的元素和索引值存入result中。

此解法的时间复杂度是O(N),空间复杂度是O(N)

public int[] sortArrayByParityII2(int[] A) {
int[] result = new int[A.length];
int j = 0;
for (int i=0; i<A.length; i++) {
if (A[i]%2 == 0) {
result[j] = A[i];
j += 2;
}
}
int k = 1;
for (int i=0; i<A.length; i++) {
if (A[i]%2 != 0) {
result[k] = A[i];
k += 2;
}
}
return result;
}

04 第三种解法

针对上面的第二种解法,我们也可以只使用一次循环。

此解法的时间复杂度是O(N),空间复杂度是O(N)

public int[] sortArrayByParityII3(int[] A) {
int[] result = new int[A.length];
int j = 0, k = 1;
for (int i=0; i<A.length; i++) {
if (A[i]%2 == 0) {
result[j] = A[i];
j += 2;
} else {
result[k] = A[i];
k += 2;
}
}
return result;
}

05 第四种解法

双指针。

定义两个指针i和j,i代表偶数索引,从0开始;j代表奇数索引,从n-1开始(n为数组A的length),如果偶数索引位置对应的元素为奇数,且奇数索引位置对应的元素为偶数,就进行元素交换。如果偶数索引位置对应的元素为偶数,偶数索引i就加2,同理,奇数索引位置对应的元素为奇数,奇数索引j就减2,循环结束条件为i不小于n或者j小于1。

此解法的时间复杂度是O(N),空间复杂度是O(1)

public int[] sortArrayByParityII4(int[] A) {
int i = 0, j = A.length-1, n = A.length;
while (i < n && j >= 1) {
if (A[i]%2 == 1 && A[j]%2 == 0) {
int tem = A[j];
A[j] = A[i];
A[i] = tem;
}
if (A[i]%2 == 0) {
i += 2;
}
if (A[j]%2 == 1) {
j -= 2;
}
}
return A;
}

06 小结

算法专题目前已连续日更超过六个月,算法题文章222+篇,公众号对话框回复【数据结构与算法】、【算法】、【数据结构】中的任一关键词,获取系列文章合集。

以上就是全部内容,如果大家有什么好的解法思路、建议或者其他问题,可以下方留言交流,点赞、留言、转发就是对我最大的回报和支持!

LeetCode.922-按奇偶排序数组 II(Sort Array By Parity II)的更多相关文章

  1. [Swift]LeetCode922.按奇偶排序数组 II | 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 ...

  2. 每日一题20201112(922. 按奇偶排序数组 II)

    题目链接: 922. 按奇偶排序数组 II 思路 很简单,搞懂问题的核心就行,假设现在有奇数在偶数位上,偶数在奇数位上. 那么我们要做的就是,找到分别在对方位置上的数字,然后交换他们就行. class ...

  3. 【LEETCODE】42、922. Sort Array By Parity II

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

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

  5. 992. Sort Array By Parity II - LeetCode

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

  6. 【Leetcode_easy】922. Sort Array By Parity II

    problem 922. Sort Array By Parity II solution1: class Solution { public: vector<int> sortArray ...

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

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

  9. 【LeetCode】922. Sort Array By Parity II 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 使用奇偶数组 排序 奇偶数位置变量 日期 题目地址: ...

随机推荐

  1. 调试python 程序的几种方法总结

    程序能一次写完并正常运行的概率很小,基本不超过1%.总会有各种各样的bug需要修正.有的bug很简单,看看错误信息就知道,有的bug很复杂,我们需要知道出错时,哪些变量的值是正确的,哪些变量的值是错误 ...

  2. sys.argv的意义及用法

    sys.argv的意义 简单来说,sys.argv是一个参数列表,这个列表存放着从外界获取到的参数(可能有多个) 下面以一个例子来详细阐述sys.argv,本次演示在ubuntu环境下运行 新建一个t ...

  3. js 多个三目运算符优先级

    读JS代码遇到一段看不懂运算优先级的代码,如下 var BrowserSys = {}; var ua = navigator.userAgent.toLowerCase(); var s; (s = ...

  4. RSA加密算法原理及RES签名算法简介(转载)

    第一部分:RSA算法原理与加密解密 一.RSA加密过程简述 A和B进行加密通信时,B首先要生成一对密钥.一个是公钥,给A,B自己持有私钥.A使用B的公钥加密要加密发送的内容,然后B在通过自己的私钥解密 ...

  5. js下拉框选择图片

    二种方式:下拉框里面选项有图片与没有图片 1.用下拉框写  下拉框的option没法添加图片如果下拉框里面不需要图片可以用这种方式. <!DOCTYPE html> <html> ...

  6. 分布式-信息方式-ActiveMQ基础

    ActiveMQ简介 ActiveMQ是什么ActiveMQ是Apache推出的,一款开源全支持JMS.1和J2EE1.4范的JMS Provider实现的信息中间件.(message oriente ...

  7. 项目配置 xml文件时 报错提示(The reference to entity "useSSL" must end with the ';' delimiter.)

    这次在配置xml文件时,出现错误提示( The reference to entity “useSSL” must end with the ‘;’ delimiter.) 报错行为 <prop ...

  8. Linux TCP套接字选项 之 SO_REUSEADDR && SO_REUSEPORT

    说明 前面从stackoverflow上找了一篇讲这两个选项的文章,文章内容很长,读到最后对Linux中的这两个选项还是有些迷茫,所以重新写一篇文章来做一个总结: 本文只总结TCP单播部分,并且只讨论 ...

  9. 作业要求20191010-9 alpha week 1/2 Scrum立会报告+燃尽图 07

    此作业要求参见:https://edu.cnblogs.com/campus/nenu/2019fall/homework/8752 一.小组情况组长:贺敬文组员:彭思雨 王志文 位军营 杨萍队名:胜 ...

  10. 选题 Scrum立会报告+燃尽图 07

    此作业要求参见:https://edu.cnblogs.com/campus/nenu/2019fall/homework/8678 一.小组情况组长:贺敬文组员:彭思雨 王志文 位军营 杨萍队名:胜 ...