Leetcode Python超琐碎笔记: 905-Sort Array By Parity

栏目: Python · 发布时间: 6年前

内容简介:若有错误之处请予以指正:)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.

问题地址 ,难度:Easy

若有错误之处请予以指正:)

问题描述

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.

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

题意分析

把偶数奇数分成前后两部分输出,空间上in-place当然最好。

我的实现及调优过程

方法1 :92 ms

暴力没什么好说的。

class Solution:
    def sortArrayByParity(self, A):
        """
        :type A: List[int]
        :rtype: List[int]
        """
        even = []
        odd = []
        for n in A:
            if n % 2 == 0:
                even.append(n)
            else:
                odd.append(n)
        return even + odd
  • 时间复杂度:O(n)
  • 空间复杂度:O(n)

方法2 :92 ms

使用列表生成式代替 for 循环,并无卵用,估计测试集太小了。

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]
  • 时间复杂度:O(n)
  • 空间复杂度:O(n)

方法3 :96 ms

In-place 方法,速度相当。直接在A上做手脚。取两个指针分别指头尾,取余后两个余数有四种情况,分别处理:

  • 前>后
  • 前<后
  • 前=后=0
  • 前=后=1
class Solution:
    def sortArrayByParity(self, A):
        """
        :type A: List[int]
        :rtype: List[int]
        """
        i, j = 0, len(A) - 1
        while i < j:
            i_re = A[i] % 2
            j_re = A[j] % 2
            # odd - even
            if i_re > j_re:
                A[i], A[j] = A[j], A[i]
                i += 1
                j -= 1
            # even - odd
            elif i_re < j_re: 
                i += 1
                j -= 1
            else:
                if i_re == 0:
                    i += 1
                else:
                    j -= 1
        return A
  • 时间复杂度:O(n)
  • 空间复杂度: O(1)

方法3-Golang版 :72 ms

刚好这两天在看Golang的基本语法,拿来耍下。快了一丢丢(居然还是个100%),然而我是完全不能理解那些 Python 下60多ms是怎么刷出来的啊啊啊,卒。

func sortArrayByParity(A []int) []int {
    i := 0
    j := len(A) - 1
    var i_re, j_re int
    for ; i<j ;{
        i_re = A[i] % 2
        j_re = A[j] % 2
        if i_re > j_re {
            c := A[i] //这里一开始还出了你懂的bug,我已经被Python惯坏
            A[i] = A[j]
            A[j] = c
            i += 1
            j -= 1
        } else if i_re < j_re { 
            i += 1
            j -= 1
        } else {
            if i_re == 0 { i += 1} else {
                j -= 1}
        }
    }
    return A
}
  • 时间复杂度:O(n)
  • 空间复杂度: O(1)

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

硅谷百年史

硅谷百年史

[美]阿伦·拉奥(Arun Rao)、[美]皮埃罗·斯加鲁菲(Piero Scarruffi) / 闫景立、侯爱华 / 人民邮电出版社 / 2014-4-1 / 99.00

一百多年来,仅硅谷就培育了50多位诺贝尔奖获得者,以及无数依靠智慧和知识而成为百万富翁的人。这一人类历史上最伟大的科技创新与创业历程为什么会发生在硅谷?究竟是如何发生的?其他地方是否可以复制出“硅谷”? 《硅谷百年史——伟大的科技创新与创业历程(1900-2013)》以编年体的顺序,从无线电技术、晶体管、集成电路,到人类基因组、互联网和云计算,详尽地记述了硅谷在100多年中所发生的重大科技事......一起来看看 《硅谷百年史》 这本书的介绍吧!

在线进制转换器
在线进制转换器

各进制数互转换器

SHA 加密
SHA 加密

SHA 加密工具