[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 <= 5000
0 <= 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 ...
随机推荐
- Linux Shell数值比较和字符串比较及相关
说明: 1. 把字符串当成整型进行比较,由于abcd等字符对不上0123当程序尝试去转成二进制时无法完成转换,所以用于数值比较的运算不能用于字符串比较:但是把整型当成字符串进行比较,0123这些数值完 ...
- openssl修改版本号
1.查看当前openssl版本号 openssl version 2.查看openssl所在位置 which openssl 3.查看保存版本号的libcrypto.so所在位置 ldd /usr/b ...
- android studio 安装步骤
1◆ jdk环境安装 2◆ android文件下载 3◆ 安装步骤 waiting --- 4◆ 配置 正在安装加速器····· google setProxy https:/ ...
- asp.net mvc如何获取url的相关信息
1.获取完整url信息(协议名+域名+虚拟目录名+文件名+参数) string url = Request.Url.ToString(); 如: //1)获取完整url(协议名+域名+虚拟目录名+文件 ...
- Matlab远程调试 转
Matlab的调试总体分为,直接调试和间接调试.1.直接调试:(1)去掉句末的分号:(2)单独调试一个函数:将第一行的函数声明注释掉,并定义输入量,以脚本方式执行 M 文件:(3)适当地方添加 ...
- [IOS微信] PList文件解析,boost数据读取
最近在解析IOS版微信数据中的 mmsetting.archive 文件时,第一次接触到PList文件. 注:mmsetting.archive 不是一个标准的PList文件,其中含有汉字,并且很多 ...
- 2-Servlet和servletContext
2018-08-09 22:34 * Servlet(好好学) * 动态WEB的资源. * 什么是Servlet * 实现Servlet接口,重写5个方法. * S ...
- flask使用配置文件
引入配置 app = Flask(__name__) app.config.from_pyfile('config.py') config.py DEBUG = True SECRET_KEY = '
- linux生成SSH key
1. 检查SSH keys是否存在 ls -al ~/.ssh2. 生成新的ssh key 输入 ssh-keygen -t rsa -C your_email@example.com
- Linux音频驱动学习之:(2)移植wm8976声卡驱动(linux-3.4.2)
1.wm8976驱动程序: /* * wm8976.h -- WM8976 Soc Audio driver * * This program is free software; you can re ...