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. 1 <= A.length <= 5000
  2. 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的更多相关文章

  1. 【Leetcode_easy】905. Sort Array By Parity

    problem 905. Sort Array By Parity solution1: class Solution { public: vector<int> sortArrayByP ...

  2. 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 ...

  3. 【LEETCODE】41、905. Sort Array By Parity

    package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * ...

  4. 905. Sort Array By Parity - LeetCode

    Question 905. Sort Array By Parity Solution 题目大意:数组排序,偶数放前,奇数在后,偶数的数之间不用管顺序,奇数的数之间也不用管顺序 思路:建两个list, ...

  5. 【LeetCode】905. Sort Array By Parity 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述: 题目大意 解题方法 自定义sorted函数的cmp 日期 题目地址:h ...

  6. [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, ...

  7. LeetCode 905 Sort Array By Parity 解题报告

    题目要求 Given an array A of non-negative integers, return an array consisting of all the even elements ...

  8. 【leetcode】905. Sort Array By Parity

    题目如下: 解题思路:本题和[leetcode]75. Sort Colors类似,但是没有要求在输入数组本身修改,所以难度降低了.引入一个新的数组,然后遍历输入数组,如果数组元素是是偶数,插入到新数 ...

  9. LeetCode 905. Sort Array By Parity 按奇偶校验排列数组

    题目 Given an array A of non-negative integers, return an array consisting of all the even elements of ...

随机推荐

  1. InnoDB存储引擎表的主键

    在InnoDB存储引擎中,表是按照主键顺序组织存放的.在InnoDB存储引擎表中,每张表都有主键(primary key),如果在创建表时没有显式地定义主键,则InnoDB存储引擎会按如下方式选择或创 ...

  2. weblogic安装教程(以weblogic 11g为例)

    1.下载jdk和weblogic安装介质 一般的搭配是jdk1.5+weblogic92.jdk1.6+weblogic11g(weblogic10.3.6) jdk历史版本下载链接:http://w ...

  3. 使用web3+solc编译发布以太坊智能合约

    一.环境安装: 1.安装web3工程:npm install web3 2.安装solc工程:npm install solc二.在node环境中使用 先引用所需环境: var fs = requir ...

  4. maven运行时的配置及命令详解

      上面是指定端口运行程序的,也可以先指定好,直接在上面的地方写jettty:run           当然,如果你是在控制台运行且安装了maven,直接可以进入项目的文件中:mvn jetty:r ...

  5. 【SQL】group by 及 having

    Group By 分组汇总 HAVING:给分组设置条件 1.概述 “Group By”从字面意义上理解就是根据“By”指定的规则对数据进行分组,所谓的分组就是将一个“数据集”划分成若干个“小区域”, ...

  6. python运算符号

    运算符 比较运算 赋值运算 逻辑运算 成员运算

  7. Spring Boot 如何极简入门?

    Spring Boot已成为当今最流行的微服务开发框架,本文是如何使用Spring Boot快速开始Web微服务开发的指南,我们将创建一个可运行的包含内嵌Web容器(默认使用的是Tomcat)的可运行 ...

  8. Linux的安装包命令/yum 与 Rpm

    1.Rpm安装包命令(以dhcp软件包为例)----Rpm安装软件包需要解决依赖性,因此特别麻烦(如图2被需要). rpm与yum安装的均为二进制软件包.类似于windows下载的软件包,可直接安装使 ...

  9. angular4-事件绑定

    事件绑定语法(可以通过 (事件名) 的语法,实现事件绑定) <date-picker (dateChanged)="statement()"></date-pic ...

  10. Cracking The Coding Interview2.4

    删除前面的linklist,使用node来表示链表 // You have two numbers represented by a linked list, where each node cont ...