题目要求

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

LeetCode 922 Sort Array By Parity II 解题报告的更多相关文章

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

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

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

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

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

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

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

  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

    题目如下: 解题思路:非常简单的题目,引入两个变量oddInx = 1和evenInx = 0,和与A等长的结果数组res.然后遍历A,如果A[i]为偶数,则令res[evenInx] = A[i], ...

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

  9. 992. Sort Array By Parity II - LeetCode

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

随机推荐

  1. 【iCore4 双核心板_ARM】例程八:定时器PWM实验——呼吸灯

    实验原理: STM32的定时器有PWM功能,iCore4的蓝色LED连接在定时器的输出接口上, 可以通过定时器的PWM输出控制LED的亮度,从而实验呼吸灯的功能. 核心代码: int main(voi ...

  2. 【LINUX】——如何配置宿主机和虚拟机IP在同一网段

    宿主机:win7  10.8.2.50 255.255.255.0 虚拟机:redhat 如果使用 NAT 的网络连接方式,虚拟机的 IP 会被分配为 192.168.*.* 网段,从虚拟机 ping ...

  3. hdoj:2033

    #include <iostream> #include <string> using namespace std; int main() { int n; int h, m, ...

  4. .Net Reactor 单个dll或exe文件的保护

    .Net  Reactor配置如下: 点一下“Protect”能执行成功,就说明配置没问题.然后保存配置文件,在vs插件上就可以直接读取使用了. vs插件配置

  5. Java如何从IP地址查找主机名?

    在Java编程中,如何从IP地址查询出主机名? 以下示例显示了如何通过net.InetAddress类的InetAddress.getByName()方法将指定的IP地址查到主机名称. package ...

  6. MAC下Myeclipse SVN插件安装

    1.下载SVN插件包:http://download.csdn.net/detail/frankyanchen/4512899 2.在myeclipse文件夹下创建一个文件夹为svntool并复制下载 ...

  7. AOP 切入点表达式

    8.切入点表达式 现在我们介绍一下最重要的切入点表达式: 如上文所说,定义切入点时需要一个包含名字和任意参数的签名,还有一个切入点表达式,就是* findById*(..)这一部分. 切入点表达式的格 ...

  8. Nginx-nginx和反向代理

    使用版本:nginx-1.10.2(windows环境使用稳定版本) 下载地址:http://nginx.org 什么是nginx? Nginx (engine x) 是一款轻量级的Web 服务器 . ...

  9. SpringBoot自定义错误信息,SpringBoot适配Ajax请求

    SpringBoot自定义错误信息,SpringBoot自定义异常处理类, SpringBoot异常结果处理适配页面及Ajax请求, SpringBoot适配Ajax请求 ============== ...

  10. 【死磕jeesite源码】jeesite添加多数据源

    本文转载自jeesite添加多数据源 1.jeesite.properties 添加数据源信息,(url2,username2,pawwword2) #mysql database setting j ...