题目如下:

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

代码如下:

class Solution(object):
def sortArrayByParityII(self, A):
"""
:type A: List[int]
:rtype: List[int]
"""
res = [0] * len(A)
oddInx = 1
evenInx = 0
for i in A:
if i % 2 == 0:
res[evenInx] = i
evenInx += 2
else:
res[oddInx] = i
oddInx += 2
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_easy】922. Sort Array By Parity II

    problem 922. Sort Array By Parity II solution1: class Solution { public: vector<int> sortArray ...

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

  4. 【leetcode】905. Sort Array By Parity

    题目如下: 解题思路:本题和[leetcode]75. Sort Colors类似,但是没有要求在输入数组本身修改,所以难度降低了.引入一个新的数组,然后遍历输入数组,如果数组元素是是偶数,插入到新数 ...

  5. 【LeetCode】905. Sort Array By Parity 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述: 题目大意 解题方法 自定义sorted函数的cmp 日期 题目地址:h ...

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

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

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

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

  9. 【Leetcode_easy】905. Sort Array By Parity

    problem 905. Sort Array By Parity solution1: class Solution { public: vector<int> sortArrayByP ...

随机推荐

  1. NET Core+win10+Jenkins+Gogs+open ssh持续集成

    背景 阿里云测试环境一台,带宽1M跟不上,Jenkins安装一个插件耗时很长,于是想在本地搭建Jenkins服务,将生成的安装文件同步到目标服务器上. 技术点有: win10:本地环境是win10,测 ...

  2. HTTP协议-Cookie和Session详解

    前言: 会话(Session)跟踪是Web程序中常用的技术,用来跟踪用户的整个会话.常用的跟踪技术就是Cookie和Session. Cookie通过在客户端记录信息确定用户身份,Session通过在 ...

  3. c++ ofstream使用方法

    ofstream是从内存到硬盘,ifstream是从硬盘到内存,流缓冲即是内存空间. 插入器<<  : 向流输出数据. cout << "test!" &l ...

  4. gitHub那些优秀的库和想要实现的效果

    1. 轮播库SDCycleScrollView 2. 自动布局库SDAutoLayout 3. 类似支付宝福卡滑动切换的效果 https://github.com/huangxuan518/HXCar ...

  5. delphi 创建桌面快捷方式

    unit UShorCut; interface uses Windows, Classes, SysUtils,Dialogs, ShlObj, ComObj, ActiveX, Registry; ...

  6. WCDB错误"No matching constructor for initialization of 'WCTColumnBinding'"

    开始看到这个错误有点匪夷所思,完全不知道问题指向哪里, 最后用过排除法,把之前建立多个类进行了一一排除,发现有个属性是 @property(nonatomic, assign) NSInteger * ...

  7. [CSP-S模拟测试]:v(hash表+期望DP)

    题目背景 $\frac{1}{4}$遇到了一道水题,又完全不会做,于是去请教小$D$.小$D$看了$0.607$眼就切掉了这题,嘲讽了$\frac{1}{4}$一番就离开了.于是,$\frac{1}{ ...

  8. vue 页面添加水印 ts

    "use strict"; // tslint:disable-next-line: only-arrow-functions const setWatermark: (str: ...

  9. SPRING CLOUD微服务DEMO-上篇

    目录 1. 微服务架构 2. 远程调用方式 2.1 RPC/RMI 2.2 Http 2.3 如何选择 3. Http客户端工具 3.1 RestTemplate 4. Spring Boot 搭建项 ...

  10. Html5 学习笔记 【PC固定布局】 实战2 导航栏搜索区域

    <!DOCTYPE html> <html lang="zh-cn"> <head> <meta charset="UTF-8& ...