【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 ...
随机推荐
- c#GridView
分页: 1.先把属性AllowPaging设置为true, 2.pagesize为每一页的行数,PageSize="15". 3.OnPageIndexChanging=" ...
- mysql数据操作语言DML
插入insert 插入方式1 语法: insert into 表名(列名,....) values(值1,....) 说明: 1.插入的值的类型要与列的类型一致或兼容 2.可以为null的值:①列写了 ...
- Linux基础命令---dig工具
dig dig是一个DNS查询工具,多数管理员会使用dig命令来解决DNS的问题. 此命令的适用范围:RedHat.RHEL.Ubuntu.CentOS.Fedora. 1.语法 di ...
- GO并发相关
锁的使用 注意要成对,重点是代码中有分支或者异常返回的情况,这种情况要在异常返回前先释放锁 mysqlInstanceLock.Lock() slaveHostSql := "show sl ...
- wsdl实例
1 <?xml version='1.0' encoding='UTF-8'?> 2 <wsdl:definitions name="HelloWorldService&q ...
- 【Linux】【Services】【SaaS】Docker+kubernetes(13. 部署Jenkins/Maven实现代码自动化发布)
1. 简介 Jenkins: 官方网站:https://jenkins.io/ 下载地址:https://jenkins.io/download/ war包下载:http://mirrors.jenk ...
- Spring Boot中使用Mybatis
一.步骤 导入依赖:MySQL驱动.Druid依赖.MyBatis与Spring Boot整合依赖.Lombok依赖 在Service接口实现类上添加@Service注解 在Dao接口上添加@Mapp ...
- 阿里云esc 安装 mysql5.7.27
1. 下载: wget http://repo.mysql.com/mysql57-community-release-el7-10.noarch.rpm 2. 安装: (1) yum -y in ...
- 1、Redis简介
一.NOSQL 1.什么是NOSQL? NoSQL(NoSQL = Not Only SQL ),意即"不仅仅是SQL". 指的是非关系型的数据库.NoSQL有时也称作Not On ...
- Jenkins凭证管理
目录 一.简介 二.管理凭证 三.常用凭证 保密文本 账号密码 保密文件 账号秘钥 四.优雅使用凭证 保密文本 账号密码 保密文件 五.凭证插件 集成HashiCorp Vault pipeline ...