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. Lamda 表达式里的Join和GroupJoin的区别, 如何实现SQL的Left Join效果

    例如,可以将产品表与产品类别表相联接,得到产品名称和与其相对应的类别名称 db.Products .Join ( db.Categories, p => p.CategoryID, c => ...

  2. 运行B/s项目时,出现尝试访问类型与数组不兼容元素问题?

    1.问题描述 运行B/s项目时,浏览器出现应用程序中服务器错误(尝试访问类型与数组不兼容的元素) 2.问题原因 本人是项目引用的dll版本不一致问题,引用的System.Web.Mvc版本是4.0.0 ...

  3. jackSon注解– @JsonInclude 注解不返回null值字段

    @Data @JsonInclude(JsonInclude.Include.NON_NULL) public class OrderDTO { private String orderId; @Js ...

  4. 蓝桥杯—BASIC-21 sine之舞(递归递推)

    题目:最近FJ为他的奶牛们开设了数学分析课,FJ知道若要学好这门课,必须有一个好的三角函数,所以他准备和奶牛们做一个“Sine之舞”的游戏,寓教于乐,提高奶牛们的计算能力. 不妨设 An=sin(1– ...

  5. 逆袭之旅DAY20.XIA.程序调试

    2018-07-16 20:25:50 F5:进入方法 F6:单步执行

  6. spark使用正则表达式读入多个文件

    String dir = "s3a://example/";String currentDir = dir + "{1[5-9],2[01]}/*.txt";J ...

  7. Ie11 的改变

    摘录地址:     http://www.4fang.net/content.jsp?id=30537 微软在上周刚刚发布了用于Windows 8.1上的首个Internet Explorer 11的 ...

  8. oracle日志相关的表

    SELECT * FROM all_objects t where  object_name like '%EN_CONCAT_IM%';DBA_HIST_SQLTEXTDBA_HIST_SQLSTA ...

  9. Bootstrap--思维导图

    Bootstrap--思维导图

  10. tf.nn.conv2d

    tf.nn.conv2d(input, filter, strides, padding, use_cudnn_on_gpu=None, name=None) input: 指需要做卷积的输入图像,它 ...