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 ...
随机推荐
- 算法复杂度图示&JavaScript算法链接
https://juejin.im/post/5c9a1d58e51d4559bb5c6694
- npm安装包时的几种模式
本文原文地址:https://www.limitcode.com/detail/59a15b1a69e95702e0780249.html 回顾 npm install 命令 最近在写Node程序的时 ...
- 【转】PowerDesigner数据库视图同时显示Code和Name
1.按顺序打开: Tools>>>Display Preference 2.依次点击 选中Code打钩,并点击箭头指向图标把Code置顶 3.最终效果图 原文链接
- Vim 入门使用
参考资料:https://www.runoob.com/linux/linux-vim.html 本篇内容不全,其余内容请参考该链接 vim/vi 是Linux下常用的文本编辑工具,它基本上有三种 ...
- 【原】简单shell练习(四)
1.查看已开启端口信息 #ss -ln 2.列出谁在使用某个端口(如:80) #lsof -i:80 3.显示文件夹下文件信息 #find /home/root -type f#find -type ...
- checkbox 选中获取值
1:jsp 页面 从页面获取checkbox 的值传到后台 <div id="divCheckbox" class="hide"> <tr&g ...
- Controller 层类
package com.thinkgem.jeesite.modules.yudengji.web; import java.util.Date; import javax.servlet.http. ...
- Windows服务器权限分析
一.Windows常见用户 二.Windows常见用组 2.1Windows常见组 2.2Windows2003常见组 三.Windows目录权限 四.Windows2003默认权限 五.不同环境下的 ...
- 「ZJOI2013」K大数查询
「ZJOI2013」K大数查询 传送门 整体二分,修改的时候用线段树代替树状数组即可. 参考代码: #include <cstdio> #define rg register #defin ...
- HTTP报文结构及Cookie、session区别
目录 万维网 HTTP超文本传输协议 特点 HTTP的报文结构. 下面介绍http请求报文最主要的一些特点 在服务器上存放用户的信息(Cookie) 实例: 工作原理: cookie中的主要内容: C ...