题目要求

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. vs2015打开慢的解决方法

        1.首先是这里,这里默认是用的软件加速,把"基于客户端性能自动调整视觉体验"去掉勾选.然后把下面的第一个选项去掉,第二选项勾选.我在想,它的"自动"基于 ...

  2. 在Thinkphp中【自动加载自定义扩展配置文件】!

    /Conf/config.php为正式的扩展文件 /Conf/verify.php为扩展的自定义配置文件 /Conf/sendmail.php为扩展的自定义配置文件 如果要自动加载 verify和se ...

  3. 为什么watch机制不是银弹?

    几乎所有构建系统都选择使用watch机制来解决开发过程中需要反复生成构建后文件的问题,但在watch机制下,长期以来我们必须忍受修改完代码,保存完代码必须喝口茶才能刷新看看效果的问题.在这里我们尝试探 ...

  4. 关于Python打包运行的一些思路

    需求 本地开发python django应用程序,然后放到生产环境运行.使用了tensorflow,手动安装包很麻烦.生产环境不能联网,不能使用 pip freeze. 思路: 使用docker,直接 ...

  5. React Native 进的第一个坑

    Bundling index.ios.js [development, non-minified, hmr disabled] 0.0% (0/1), failed. error: bundling ...

  6. hello alibaba

    http://ifeve.com/dubbo-learn-book/ http://ifeve.com/leader-follower-thread-model/ http://ifeve.com/a ...

  7. Ubuntu 14.04 安装 Blender,并放桌面

    1.打开Blender官网 2.在主页点击down按钮,跳转到下载页面 3.根据自己的操作系统,选择合适的版本,这里使用的是ubunut 64位,所以下载Linux 64bit,保存文件,一般保存到h ...

  8. Rest风格理解

    之前一直不理解restful风格,今天终于理解了些(20170527) 正常我们在浏览器的地址栏中输入的地址很多都是发起的,发起的都是get请求 通过ajax可以设置put请求,F12查看浏览器请求头 ...

  9. session一直报错Session store not set on request

    Route::group(['middleware' => ['web']], function () { //});仍然报错,看了 session是使用默认file,没问题:app/stora ...

  10. JAVA程序员_常用英语

    干程序员这行实在是离不开英语,干程序员是一项很辛苦的工作,要成为一个高水平的程序员尤为艰难.这是因为计算机软件技术更新的速度越来越快,而这些技术大多来源于英语国家,我们在引进这些技术时往往受到语言障碍 ...