题目要求

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 解题报告相关推荐

  1. Leetcode PHP题解--D16 922. Sort Array By Parity II

    2019独角兽企业重金招聘Python工程师标准>>> 922. Sort Array By Parity II 题目链接 922. Sort Array By Parity II ...

  2. 992. Sort Array By Parity II - LeetCode

    为什么80%的码农都做不了架构师?>>>    Question 992. Sort Array By Parity II Solution 题目大意:给一个int数组,一半是奇数一 ...

  3. 【LeetCode】954. Array of Doubled Pairs 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  4. 【LeetCode】487. Max Consecutive Ones II 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetco ...

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

  6. Leetcode PHP题解--D7 905. Sort Array By Parity

    2019独角兽企业重金招聘Python工程师标准>>> 905. Sort Array By Parity 题目链接 905. Sort Array By Parity 题目分析 这 ...

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

  8. LeetCode 167.Two Sum II 解题报告

    LeetCode 167.Two Sum II 解题报告 题目描述 Given an array of integers that is already sorted in ascending ord ...

  9. 【LeetCode】222. Count Complete Tree Nodes 解题报告(Python)

    [LeetCode]222. Count Complete Tree Nodes 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个 ...

最新文章

  1. Ubuntu 显示隐藏文件
  2. Centos6.5下docker 环境搭建
  3. PyTorch GPU安装指南 (Ubuntu 16.04 anaconda cuda8.0 cuDNN6.0)
  4. Java泛型教程–示例类,接口,方法,通配符等
  5. Map以自定义类做为键值
  6. Signal和Slot是同步的还是异步的
  7. blender源代码分析----第三方库的说明
  8. catia制作物料明细_CATIA课时:动力头主架详细标注及物料清单视频教程_翼狐网...
  9. Panabit标准版免费版功能限制
  10. Spring+Struts2+Hibernate概述
  11. 解决 注册谷 歌 邮 箱 gmail 手机号无法用于验证
  12. 容抗 感抗 初级计算公式
  13. Excel VBA 外贸邮件群发
  14. 不规律的生活有什么危害
  15. 一个程序员的丰功伟绩
  16. 职业生涯规划计算机专业中职生3000字,计算机专业职业生涯规划书范文格式3000字...
  17. 人口普查也是一个超大的“用户画像”。
  18. java通过ftl模板导出word最详细教程
  19. Spring In Action读书笔记
  20. ubuntu安装解压版mysql数据库

热门文章

  1. Android开发必看知识,不看后悔
  2. Linux Lernel Panic 报错解决思路
  3. debian6 xen4.0安装 guest半虚拟化--tar安装
  4. java 数据存储到什么地方?
  5. 转载:VMware虚拟机时钟不准的问题(linux图形界面投影到windows配置参考)--略有修改...
  6. 1000 qps java,什么是QPS?
  7. boot.asm文件注释
  8. linux中的fg命令
  9. spring31-1: 事务-传播行为
  10. mybaits十七:使用foreach标签