【LeetCode】922. Sort Array By Parity II 解题报告(Python)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址: https://leetcode.com/problems/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 integers are even.
Sort the array so that whenever A[i] is odd, i is odd; and whenever A[i] is even, i is even.
You may return any answer array that satisfies this condition.
Example 1:
Input: [4,2,5,7]
Output: [4,5,2,7]
Explanation: [4,7,2,5], [2,5,4,7], [2,7,4,5] would also have been accepted.
Note:
- 2 <= A.length <= 20000
- A.length % 2 == 0
- 0 <= A[i] <= 1000
题目大意
把一个数组重新排序,使得偶数位置全是偶数,奇数位置全是奇数。
解题方法
使用奇偶数组
直接使用两个数组分别存放奇数和偶数,然后结果就是在这两个里面来回的选取就好了。这种做法比较简单,打比赛比较适用。
时间复杂度是O(N),空间复杂度是O(N)。
class Solution(object):
def sortArrayByParityII(self, A):
"""
:type A: List[int]
:rtype: List[int]
"""
odd = [x for x in A if x % 2 == 1]
even = [x for x in A if x % 2 == 0]
res = []
iseven = True
while odd or even:
if iseven:
res.append(even.pop())
else:
res.append(odd.pop())
iseven = not iseven
return res
排序
先对A进行排序,使得偶数都在前面,奇数都在后面,然后每次从前从后各取一个数,然后放到结果里就好了。
class Solution:
def sortArrayByParityII(self, A):
"""
:type A: List[int]
:rtype: List[int]
"""
A.sort(key = lambda x : x % 2)
N = len(A)
res = []
for i in range(N // 2):
res.append(A[i])
res.append(A[N - 1 - i])
return res
时间复杂度是O(NlogN),空间复杂度是O(1)。
奇偶数位置变量
先把结果数组创建好,然后使用奇偶数两个变量保存位置,然后判断A中的每个数字是奇数还是偶数,对应放到奇偶位置就行了。
时间复杂度是O(N),空间复杂度是O(1)。
class Solution:
def sortArrayByParityII(self, A):
"""
:type A: List[int]
:rtype: List[int]
"""
N = len(A)
res = [0] * N
even, odd = 0, 1
for a in A:
if a % 2 == 1:
res[odd] = a
odd += 2
else:
res[even] = a
even += 2
return res
时间复杂度是O(N),空间复杂度是O(N)。
参考资料:
日期
2018 年 10 月 14 日 —— 周赛做出来3个题,开心
2018 年 11 月 5 日 —— 打了羽毛球,有点累
【LeetCode】922. Sort Array By Parity II 解题报告(Python)的更多相关文章
- 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 i ...
- 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] 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
https://leetcode.com/problems/sort-array-by-parity-ii/ Given an array A of non-negative integers, ha ...
- 【LEETCODE】42、922. Sort Array By Parity II
package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * ...
- 【Leetcode_easy】922. Sort Array By Parity II
problem 922. Sort Array By Parity II solution1: class Solution { public: vector<int> sortArray ...
- 【leetcode】922. Sort Array By Parity II
题目如下: 解题思路:非常简单的题目,引入两个变量oddInx = 1和evenInx = 0,和与A等长的结果数组res.然后遍历A,如果A[i]为偶数,则令res[evenInx] = A[i], ...
- 【leetocde】922. Sort Array By Parity II
Given an array of integers nums, half of the integers in nums are odd, and the other half are even. ...
- 【LeetCode】92. Reverse Linked List II 解题报告(Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 迭代 递归 日期 题目地址:https://leet ...
随机推荐
- Python编译工具Anaconda(含有spyder+jupyter)
Anaconda的下载和安装 官方的下载地址:https://www.anaconda.com/distribution/ 安装程序为一个可执行程序文件,下载完成后双击执行程序即可完成安装.安装过程一 ...
- 浅谈Facebook的服务器架构
导读:毫无疑问,作为全球最领先的社交网络,Facebook的高性能集群系统承担了海量数据的处理,它的服务器架构一直为业界众人所关注.CSDN博主yanghehong在他自己最新的一篇博客< Fa ...
- 用python写的推箱子搜索程序
1 # -*- coding: gbk -*- 2 from functools import reduce 3 from copy import deepcopy 4 import re 5 def ...
- day04 Linux基础命令
day04 Linux基础命令 查看帮助信息命令 1.man命令:man命令的功能是查看指定命令的详细解释. 格式:man [具体需要被查看的命令] [root@localhost ~]# man r ...
- nodejs-Cluster模块
JavaScript 标准参考教程(alpha) 草稿二:Node.js Cluster模块 GitHub TOP Cluster模块 来自<JavaScript 标准参考教程(alpha)&g ...
- Shell学习(八)——dd命令
一.dd命令的解释 dd:用指定大小的块拷贝一个文件,并在拷贝的同时进行指定的转换. 注意:指定数字的地方若以下列字符结尾,则乘以相应的数字:b=512:c=1:k=1024:w=2 参数注释: 1. ...
- Android中的性能优化
由于手机硬件的限制,内存和CPU都无法像pc一样具有超大的内存,Android手机上,过多的使用内存,会容易导致oom,过多的使用CPU资源,会导致手机卡顿,甚至导致anr.我主要是从一下几部分进行优 ...
- mysql锁相关讲解及其应用
一.mysql的锁类型 了解Mysql的表级锁 了解Mysql的行级锁 (1) 共享/排它锁(Shared and Exclusive Locks) 共享锁和排他锁是InnoDB引擎实现的标准行级别锁 ...
- SVN终端演练-版本回退
1. 版本回退概念以及原因? 概念: 是指将代码(本地代码或者服务器代码), 回退到之前记录的某一特定版本 原因: 如果代码做错了, 想返回之前某个状态重做;2. 修改了,但未提交的情况下 ...
- InnoDB学习(三)之BinLog
BinLog又称为二进制日志,是MySQL服务层的数据日志,MySQL所有的存储引擎都支持BinLog.BinLog记录了MySQL中的数据更新和可能导致数据更新的事件,可以用于主从复制或数据恢复.本 ...