"""
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.
Example 1:
Input: [4,2,5,7]
Output: [4,5,2,7]
Explanation: [4,7,2,5], [2,5,4,7], [2,7,4,5] would also have been accepted.
"""
"""
提供三种方法,传送门:https://blog.csdn.net/fuxuemingzhu/article/details/83045735
1.直接使用两个数组分别存放奇数和偶数,然后结果就是在这两个里面来回的选取就好了。
""" class Solution1:
def sortArrayByParityII(self, A):
odd = [x for x in A if x % 2 == 1] #奇数入栈
even = [x for x in A if x % 2 == 0] #偶数入栈
res = []
iseven = True #!!!判断奇偶数
while odd or even:
if iseven:
res.append(even.pop()) #偶数写入结果
else:
res.append(odd.pop()) #奇数写入结果
iseven = not iseven #!!!下一个变为偶数
return res """
先对A进行排序,使得偶数都在前面,奇数都在后面,
然后每次从前从后各取一个数,然后放到结果里就好了
"""
class Solution2:
def sortArrayByParityII(self, A):
A.sort(key=lambda x: x % 2)#!!!此方法可以将偶数防前面,奇数放后面 [0, 1]排序
N = len(A)
res = []
for i in range(N // 2):
#" / "就表示 浮点数除法,返回浮点结果;" // "表示整数除法。
res.append(A[i]) #添加偶数
res.append(A[N - 1 - i]) #添加奇数
return res """
先把结果数组创建好,
然后使用奇偶数两个变量保存位置,
然后判断A中的每个数字是奇数还是偶数,
对应放到奇偶位置就行了。
"""
class Solution3:
def sortArrayByParityII(self, A):
"""
:type A: List[int]
:rtype: List[int]
"""
N = len(A)
res = [0] * N
even, odd = 0, 1
for a in A:
if a % 2 == 1:
res[odd] = a
odd += 2 #!!!关键点,每次加2
else:
res[even] = a
even += 2 #!!!
return res

leetcode922 Sort Array By Parity II的更多相关文章

  1. Leetcode922.Sort Array By Parity II按奇偶排序数组2

    给定一个非负整数数组 A, A 中一半整数是奇数,一半整数是偶数. 对数组进行排序,以便当 A[i] 为奇数时,i 也是奇数:当 A[i] 为偶数时, i 也是偶数. 你可以返回任何满足上述条件的数组 ...

  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】42、922. Sort Array By Parity II

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

  4. 【Leetcode_easy】922. Sort Array By Parity II

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

  5. 992. Sort Array By Parity II - LeetCode

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

  6. Sort Array By Parity II LT922

    Given an array A of non-negative integers, half of the integers in A are odd, and half of the intege ...

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

  8. [Swift]LeetCode922.按奇偶排序数组 II | 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 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 i ...

随机推荐

  1. IELTS Simon wr task p3

  2. java 实现用户自由选择字段实现导出EXCEL表格

    package com.thinkgem.jeesite.common.utils.excel; import java.io.File; import java.io.OutputStream; i ...

  3. MongoDB基础篇1:安装和服务配置

    一.下载 请前往官网下载community版本MongoDB,我当前可见最新版本是3.6.4 https://www.mongodb.com/download-center#community 如需下 ...

  4. Linux 允许root用户远程登陆

    首先确保ssh服务已经安装: ps -e | grep ssh or service ssh start 如果没有安装则: apt-get install ssh 安装完之后 查看 /etc/ssh/ ...

  5. Spring Boot整合Mybatis(注解方式和XML方式)

    其实对我个人而言还是不够熟悉JPA.hibernate,所以觉得这两种框架使用起来好麻烦啊. 一直用的Mybatis作为持久层框架, JPA(Hibernate)主张所有的SQL都用Java代码生成, ...

  6. SpringBoot 处理异常的几种常见姿势

    SpringBoot 处理异常的几种常见姿势 1. 使用 @ControllerAdvice 和 @ExceptionHandler 处理全局异常 这是目前很常用的一种方式,非常推荐.测试代码中用到了 ...

  7. CentOS 下的apache服务器配置与管理

    一.WEB服务器与Apache1.web服务器与网址 2.Apache的历史 3.补充http://www.netcraft.com/可以查看apache服务器的市场占有率同时必须注意的是ngnix, ...

  8. 设计模式课程 设计模式精讲 10-2 外观模式coding

    1 代码演练 1.1 代码演练1(不使用外观模式) 1.2 代码演练2(使用外观模式) 2 应用核心 2.1 核心 2.2 零碎知识点 1 代码演练 1.1 代码演练1(不使用外观模式) 需求: 木木 ...

  9. 142、Java内部类之在普通方法里面定义内部类

    01.代码如下: package TIANPAN; class Outer { // 外部类 private String msg = "Hello World !"; publi ...

  10. 120、Java中String类之实现首字母大写

    01.代码如下: package TIANPAN; /** * 此处为文档注释 * * @author 田攀 微信382477247 */ public class TestDemo { public ...