"""
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. 1014 Waiting in Line (30分)

    1014 Waiting in Line (30分)   Suppose a bank has N windows open for service. There is a yellow line i ...

  2. Atom 必装插件

    Atom 必装插件 转载请注明出处. https://blog.csdn.net/Nick_php/article/details/54020956 主题 atom-material-ui 字体配色 ...

  3. StringBuffer调整空间

    在无法估算字符串大小情况下,可以使用StringBuffer的trimToSize()方法调整到合适大小.

  4. 【原】postman常用设置全局变量的js片段

    postman知识总结: API自动化利器:http://www.bayescafe.com/tools/use-postman-to-test-api-automatically.html 1.获取 ...

  5. [原]HelloWorld

    几乎所有程序员的编程都是从写HelloWorld开始的,作为新开的Blog我还是照旧吧. 首先需要肯定的是博客园的管理员做事很高效,我是22:08申请的,结果22:32就审核通过了,理论上讲申请审核时 ...

  6. 解题报告:luogu P2220

    指挥使走后一脸懵逼,然后想起了一道水\(SB\)的省选题. 这是毒瘤乘法分配率的应用,似乎还有一篇,算是入门题. 对了,这题连接:P2220 [HAOI2012]容易题 然而蒟蒻还是先自闭了一会... ...

  7. XML简单介绍

    什么是 XML? XML 指可扩展标记语言(EXtensible Markup Language). XML 是一种很像HTML的标记语言. XML 的设计宗旨是传输数据,而不是显示数据. XML 标 ...

  8. 109、Java中String类之截取部分子字符串

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

  9. js中每隔一段时间执行一次

    window.setInterval("flushs()",1000); 

  10. idea2019 3.3最新版本破解安装教程

    直接给上神秘地址得了:(应该都可以破解) https://www.jianshu.com/p/c7bdc5819d31