leetcode922 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 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的更多相关文章
- Leetcode922.Sort Array By Parity II按奇偶排序数组2
给定一个非负整数数组 A, A 中一半整数是奇数,一半整数是偶数. 对数组进行排序,以便当 A[i] 为奇数时,i 也是奇数:当 A[i] 为偶数时, i 也是偶数. 你可以返回任何满足上述条件的数组 ...
- 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 ...
- 【LEETCODE】42、922. Sort Array By Parity II
package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * ...
- 【Leetcode_easy】922. Sort Array By Parity II
problem 922. Sort Array By Parity II solution1: class Solution { public: vector<int> sortArray ...
- 992. Sort Array By Parity II - LeetCode
Question 992. Sort Array By Parity II Solution 题目大意:给一个int数组,一半是奇数一半是偶数,分别对偶数数和奇数数排序并要求这个数本身是偶数要放在偶数 ...
- 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 ...
- [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 ...
- [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 ...
- 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 ...
随机推荐
- js 原生url编码
参考:http://www.runoob.com/jsref/jsref-decodeuricomponent.html
- Checked exceptions: Java’s biggest mistake-检查型异常:Java最大的错误(翻译)
原文地址:http://literatejava.com/exceptions/checked-exceptions-javas-biggest-mistake/ 仅供参考,毕竟我四级都没过 Chec ...
- 【PAT甲级】1028 List Sorting (25 分)
题意: 输入一个正整数N(<=100000)和C(C属于{1,2,3}),接下来输入N行,每行包括学生的六位学号(习惯用string输入,因为可能有前导零),名字和成绩(正整数).输出排序后的信 ...
- 转--Spring MVC : Java模板引擎 Thymeleaf (三)
原文:http://www.csdn.com/html/topnews201408/49/1349.htm 下面以构造一个表单开始,讲解 Thymeleaf的用法.为了演示方便,还是以经典的注册为例. ...
- R rep() 函数
函数 rep(x,...) rep(x,times = n) 将向量 x 重复 n 次 rep(x,each = n) 将向量 x 的每个元素重复 n 次 在参数缺省情况下,为参数 times
- Golang mysql数据库
基本操作: Open() – create a DB Close() - close the DB Query() - 查询 QueryRow() -查询行 Exec() -执行操作,update,i ...
- Mac brew安装的php修改了php.ini之后如何重启php?
环境:nginx+mysql+php7.2:Mac利用homebrew安装的php7.2 问题:修改了PHP的配置文件,php.ini:服务器是nginx,如何重启PHP? 开启: brew serv ...
- pycharm安装PyQt框架
首先需要安装必要插件 File --> Settings --> Project Interpreter 如果出现以下这种情况:请升级pip,参考:https://www.cnbl ...
- js 字符串 常用处理方式(检索、截取、拼接、批量替换)
// 检索(字符串中判断是否包含某个字符) 字符串.search('检索的内容');// 返回-1,不包含: 返回非-1,包含 字符串.indexOf("待判断的内容"); // ...
- SpringCloud实战——(2)通过Feign调用其他模块
大型项目下往往有很多模块,ZCGL项目结构如下: 需要引用的其他模块已经发布成服务并在Eureka Server注册中心注册,如下: 写程序时引用了其他模块,并且其他模块在项目中,但是IDEA任然无法 ...