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 ...
随机推荐
- win10系统黑屏无法显示桌面解决
适用情况:win10系统 黑屏无法显示出桌面但是程序能正常运行时 解决方法:win+r 调出运行窗口 运行:Explorer.exe
- kali apt update 错误——下列签名无效: EXPKEYSIG ED444FF07D8D0BF6 Kali Linux Repository
这是因为key过期了 wget -q -O - https://archive.kali.org/archive-key.asc | apt-key add apt update
- 吴裕雄 Bootstrap 前端框架开发——Bootstrap 显示代码:电脑程序输出: Sample output
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- Session共享解决方案
使用nginx做的负载均衡添加一个ip_hash配置 一.开两个Tomcat写测试程序 @WebServlet("/nginxSessionServlet") public cla ...
- js缓慢滑块
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- vs2013中配置SQLite数据库
转载:https://maplefan.com/index.php/2019/08/14/visual-studio-2013%e9%85%8d%e7%bd%aesqlite3%e7%9a%84%e6 ...
- Intellij IDEA 快捷键 与 环境设置
快捷键 Ctrl+Shift+F10,运行 Ctrl+Alt+O,导入包,自动修正 Ctrl+Alt+L,格式化代码 Ctrl+Y,删除一整行 Alt + Insert,生成get/set方法 Ctr ...
- JDBC 操作插入表出现javax.sql.rowset.serial.SerialBlob cannot be cast to oracle.sql.BLOB
/** * 接口方法 */ public void excuteInputDB(SynchServiceConfig synchServiceConfig) throws Exception { tr ...
- ch8 让div居中--使用外边距
假设有一个布局,希望让其中的容器div在屏幕上水平居中,则只需要定义div的宽度,然后将水平外边距设置为auto <body> <div class="wrapper&qu ...
- 吴裕雄 Bootstrap 前端框架开发——Bootstrap 按钮:为按钮添加基本样式
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...