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 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.
题目分析及思路
题目给出一个整数数组A,其中一半是奇数,一半是偶数,要求重新排序数组,索引为奇数的是奇数,索引为偶数的是偶数。可以分别获取奇数列表和偶数列表,然后遍历索引,依次取值。
python代码
class Solution:
def sortArrayByParityII(self, A: 'List[int]') -> 'List[int]':
odd = [i for i in A if i % 2 == 1]
even = [i for i in A if i % 2 == 0]
res = []
for i in range(len(A)):
if i % 2 == 1:
res.append(odd.pop())
else:
res.append(even.pop())
return res
转载于:https://www.cnblogs.com/yao1996/p/10357160.html
LeetCode 922 Sort Array By Parity II 解题报告相关推荐
- Leetcode PHP题解--D16 922. Sort Array By Parity II
2019独角兽企业重金招聘Python工程师标准>>> 922. Sort Array By Parity II 题目链接 922. Sort Array By Parity II ...
- 992. Sort Array By Parity II - LeetCode
为什么80%的码农都做不了架构师?>>> Question 992. Sort Array By Parity II Solution 题目大意:给一个int数组,一半是奇数一 ...
- 【LeetCode】954. Array of Doubled Pairs 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】487. Max Consecutive Ones II 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetco ...
- [Leetcode] 240. Search a 2D Matrix II 解题报告
题目: Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the f ...
- Leetcode PHP题解--D7 905. Sort Array By Parity
2019独角兽企业重金招聘Python工程师标准>>> 905. Sort Array By Parity 题目链接 905. Sort Array By Parity 题目分析 这 ...
- LeetCode 905 Sort Array By Parity--Java stream,Python lambda表达式一行 解法
题目地址:Sort Array By Parity - LeetCode Given an array A of non-negative integers, return an array cons ...
- LeetCode 167.Two Sum II 解题报告
LeetCode 167.Two Sum II 解题报告 题目描述 Given an array of integers that is already sorted in ascending ord ...
- 【LeetCode】222. Count Complete Tree Nodes 解题报告(Python)
[LeetCode]222. Count Complete Tree Nodes 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个 ...
最新文章
- Ubuntu 显示隐藏文件
- Centos6.5下docker 环境搭建
- PyTorch GPU安装指南 (Ubuntu 16.04 anaconda cuda8.0 cuDNN6.0)
- Java泛型教程–示例类,接口,方法,通配符等
- Map以自定义类做为键值
- Signal和Slot是同步的还是异步的
- blender源代码分析----第三方库的说明
- catia制作物料明细_CATIA课时:动力头主架详细标注及物料清单视频教程_翼狐网...
- Panabit标准版免费版功能限制
- Spring+Struts2+Hibernate概述
- 解决 注册谷 歌 邮 箱 gmail 手机号无法用于验证
- 容抗 感抗 初级计算公式
- Excel VBA 外贸邮件群发
- 不规律的生活有什么危害
- 一个程序员的丰功伟绩
- 职业生涯规划计算机专业中职生3000字,计算机专业职业生涯规划书范文格式3000字...
- 人口普查也是一个超大的“用户画像”。
- java通过ftl模板导出word最详细教程
- Spring In Action读书笔记
- ubuntu安装解压版mysql数据库