[LeetCode&Python] Problem 905: Sort Array By Parity
Given an array A of non-negative integers, return an array consisting of all the even elements of A, followed by all the odd elements of A.
You may return any answer array that satisfies this condition.
Example 1:
Input: [3,1,2,4]
Output: [2,4,3,1]
The outputs [4,2,3,1], [2,4,1,3], and [4,2,1,3] would also be accepted.
Note:
1 <= A.length <= 50000 <= A[i] <= 5000
Try:
In the beginning, I want to use recursive method to solve this problem.
class Solution:
def sortArrayByParity(self, A):
"""
:type A: List[int]
:rtype: List[int]
""" if not A: return []
if A[0]%2!=0:
return self.sortArrayByParity(A[1:])+[A[0]]
return [A[0]]+self.sortArrayByParity(A[1:])
The problem is that this method needs too much memory space.
Solution:
Just use one line, you can solve this problem.
class Solution:
def sortArrayByParity(self, A):
"""
:type A: List[int]
:rtype: List[int]
"""
return [x for x in A if x%2==0]+[x for x in A if x%2!=0]
[LeetCode&Python] Problem 905: Sort Array By Parity的更多相关文章
- 【Leetcode_easy】905. Sort Array By Parity
problem 905. Sort Array By Parity solution1: class Solution { public: vector<int> sortArrayByP ...
- LeetCode 905. Sort Array By Parity
905. Sort Array By Parity Given an array A of non-negative integers, return an array consisting of a ...
- 【LEETCODE】41、905. Sort Array By Parity
package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * ...
- 905. Sort Array By Parity - LeetCode
Question 905. Sort Array By Parity Solution 题目大意:数组排序,偶数放前,奇数在后,偶数的数之间不用管顺序,奇数的数之间也不用管顺序 思路:建两个list, ...
- 【LeetCode】905. Sort Array By Parity 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述: 题目大意 解题方法 自定义sorted函数的cmp 日期 题目地址:h ...
- [LeetCode] 905. Sort Array By Parity 按奇偶排序数组
Given an array A of non-negative integers, return an array consisting of all the even elements of A, ...
- LeetCode 905 Sort Array By Parity 解题报告
题目要求 Given an array A of non-negative integers, return an array consisting of all the even elements ...
- 【leetcode】905. Sort Array By Parity
题目如下: 解题思路:本题和[leetcode]75. Sort Colors类似,但是没有要求在输入数组本身修改,所以难度降低了.引入一个新的数组,然后遍历输入数组,如果数组元素是是偶数,插入到新数 ...
- LeetCode 905. Sort Array By Parity 按奇偶校验排列数组
题目 Given an array A of non-negative integers, return an array consisting of all the even elements of ...
随机推荐
- nodejs利用sequelize-auto 根据数据库的table 生成model
1.打开cmd命令窗口,安装sequelize-auto npm install -g sequelize-auto 在使用sequelize-auto之前需要安装全局的mysql(举例mysql) ...
- JBOSS禁用delete和put方法教程
一.背景说明(与此节修复没多大关系可跳过) 今天应用报扫描出“启用不安全的HTTP方法”漏洞需要进行修复,看后边还有IIS的修复建议:一边不满怎么用IIS一边研究了具体操作半天,由于IIS不同版本操作 ...
- Tomcat压缩传输设置
1.配置位于server.xml文件中的Connector节点下,具体参数如下: 参数 默认值 参数说明 compression off 是否开启压缩传输 compressableMimeType t ...
- install the Mondo Rescue utility in Ubuntu 12.04 or 12.10.
1. Open a terminal window. 2. Type in the following commands, then hit Enter after each. wget ft ...
- nyoj 1091 还是01背包(超大数dp)
nyoj 1091 还是01背包 描述 有n个重量和价值分别为 wi 和 vi 的物品,从这些物品中挑选总重量不超过W的物品,求所有挑选方案中价值总和的最大值 1 <= n <=40 1 ...
- Win10系列:VC++ Direct3D模板介绍3
(4)Render函数 默认定义在CubeRenderer.cpp源文件中的Render函数用于绘制立体图形,此函数的实现代码如下所示: void CubeRenderer::Render() { ...
- POJ 1840 Eqs 解方程式, 水题 难度:0
题目 http://poj.org/problem?id=1840 题意 给 与数组a[5],其中-50<=a[i]<=50,0<=i<5,求有多少组不同的x[5],使得a[0 ...
- 每天CSS学习之color
color意为颜色,其作用是设置字体的颜色. 设置颜色的方式有以下几种: 1.使用颜色单词,如:red: 2.使用十六进制,如:#FFFFFF: 3.使用rgb(255,0,0); 让我们来实践一下, ...
- PC/FORTH 数字类型
body, table{font-family: 微软雅黑} table{border-collapse: collapse; border: solid gray; border-width: 2p ...
- Linux文件系统中的inode节点详细介绍
这篇文章主要介绍了Linux文件系统中的inode节点,详细讲解了inode是什么.inode包含的信息.inode号码的相关资料等,需要的朋友可以参考下 一.inode是什么? 理解inode,要从 ...