最近,在用解决LeetCode问题的时候,做了349: Intersection of Two Arrays这个问题,就是求两个列表的交集。我这种弱鸡,第一种想法是把问题解决,而不是分析复杂度,于是写出了如下代码:

 class Solution(object):
def intersection(self, nums1, nums2):
"""
:type nums1: List[int]
:type nums2: List[int]
:rtype: List[int]
"""
ret = []
for i in nums1:
if i in nums2 and not i in ret:
ret.append(i)
return ret

打眼一看,嗯,挺好,时间负责度是O(n),点击提交,AC;打开结果一看,EXM?才击败了15%?这是O(n*2)的复杂度啊!哪里有问题呢?再一看,问题就出在i in nums2这个语句中了,在Python中,List的in操作的时间复杂度是O(n),也就是实现的算法复杂度果然是O(n2)了。看来只是单纯的看表面的循环的复杂度是不行的,还是要了解一些内部的实现。等等,这是两个列表的交集操作啊,集合才是完美的实现,python自带了集合类型set。嗯,就用集合了,又写了如下代码:

 class Solution(object):
def intersection(self, nums1, nums2):
"""
:type nums1: List[int]
:type nums2: List[int]
:rtype: List[int]
"""
return list(set(nums1).intersection(set(nums2)))

提交,AC,查看结果,beat 80%,果然快了好多,代码还更加Pythonic了。对于Python的各种数据类型的时间复杂度,可以看这里。写代码的过程中,要充分了解Python的内部实现,才能运行的更快啊!

然后又看到了350. Intersection of Two Arrays II,这次的结果是两个数组的交集,但是可以有重复元素了,要运行O(n)的话,这次直接想到了用空间换时间,无非是使用hash了,Python的字典就是hash实现的,于是写了:

 class Solution(object):
def intersect(self, nums1, nums2):
"""
:type nums1: List[int]
:type nums2: List[int]
:rtype: List[int]
"""
tmp_dict = dict()
ret = []
for i in nums1:
tmp_dict[i] = tmp_dict[i] + 1 if tmp_dict.get(i) else 1
for n in nums2:
if tmp_dict.get(n) > 0:
ret.append(n)
tmp_dict[n] -= 1
return ret

提交运行,果然,击败了90%。结果不错,但是我还是想到用Python的Countrs了,这样会不会更快呢?点击打开讨论区,果然看到有这样用的:

 class Solution(object):
def intersect(self, nums1, nums2):
"""
:type nums1: List[int]
:type nums2: List[int]
:rtype: List[int]
"""
a, b = map(collections.Counter, (nums1, nums2))
return list((a & b).elements())

代码更短了,对于熟悉Counters的人来说,也更好理解了,不过运行效率也没有提升。至于哪种方式好,就是另外一个问题了。

Python 解LeetCode:Intersection of Two Arrays的更多相关文章

  1. [LeetCode] Intersection of Two Arrays II 两个数组相交之二

    Given two arrays, write a function to compute their intersection. Example:Given nums1 = [1, 2, 2, 1] ...

  2. [LeetCode] Intersection of Two Arrays 两个数组相交

    Given two arrays, write a function to compute their intersection. Example:Given nums1 = [1, 2, 2, 1] ...

  3. LeetCode Intersection of Two Arrays

    原题链接在这里:https://leetcode.com/problems/intersection-of-two-arrays/ 题目: Given two arrays, write a func ...

  4. LeetCode Intersection of Two Arrays II

    原题链接在这里:https://leetcode.com/problems/intersection-of-two-arrays-ii/ 题目: Given two arrays, write a f ...

  5. [LeetCode&Python] Problem 350. Intersection of Two Arrays II

    Given two arrays, write a function to compute their intersection. Example 1: Input: nums1 = [1,2,2,1 ...

  6. [LeetCode&Python] Problem 349. Intersection of Two Arrays

    Given two arrays, write a function to compute their intersection. Example 1: Input: nums1 = [1,2,2,1 ...

  7. 【leetcode❤python】350. Intersection of Two Arrays II

    #-*- coding: UTF-8 -*- class Solution(object):    def intersect(self, nums1, nums2):                ...

  8. Python 解leetcode:48. Rotate Image

    题目描述:把一个二维数组顺时针旋转90度: 思路: 对于数组每一圈进行旋转,使用m控制圈数: 每一圈的四个元素顺时针替换,可以直接使用Python的解包,使用k控制每一圈的具体元素: class So ...

  9. Python 解LeetCode:671. Second Minimum Node In a Binary Tree

    题目在这里,要求一个二叉树的倒数第二个小的值.二叉树的特点是父节点的值会小于子节点的值,父节点要么没有子节点,要不左右孩子节点都有. 分析一下,根据定义,跟节点的值肯定是二叉树中最小的值,剩下的只需要 ...

随机推荐

  1. P60 2.6

    import java.util.Scanner; public class Num { public static void main(String[] args) { Scanner input ...

  2. es6零基础学习之项目目录创建(一)

    和大家分享一下在学习es6的过程中所积累的东西,也希望更多的朋友能够互相学习 首先创建项目目录 打开你的命令行,什么文件下都可以,大家请随意,我自己用的git,输入 mkdir es6 创建一个完整的 ...

  3. C# Async/await 异步多线程编程

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.N ...

  4. hdu5696 区间的价值

    区间的价值 我们定义"区间的价值"为一段区间的最大值*最小值. 一个区间左端点在L,右端点在R,那么该区间的长度为(R-L+1). 现在聪明的杰西想要知道,对于长度为k的区间,最大 ...

  5. Java中public,protected,default,private的访问权限问题(简明扼要)

    import packa.*;//导入了packa包中所有的类.(不包括包中的子包)一般不会用,用哪个导入哪个. 导包的原则:用到哪个类,就导入哪个类.所有字母都小写. 权限列表:   public ...

  6. win10 uwp 模拟网页输入

    有时候需要获得网页的 js 执行后的源代码,或者模拟网页输入,如点按钮输入文字. 如果需要实现,那么就需要用 WebView ,使用方法很简单. 首先创建一个 WebView ,接下来的所有输入都需要 ...

  7. win10 uwp 异步进度条

    本文主要讲我设计的几个进度条,还有如何使用异步控制进度条,如何使用动画做进度. 进度条可以参见:http://edi.wang/post/2016/2/25/windows-10-uwp-modal- ...

  8. CentOS 6.2下搭建Web服务器

    1Centos 6.2下搭建web服务器 如今,Linux在Web应用越来越广,许多企业都采用Linux来搭建Web服务器,这样即节省了购买正版软件的费用,而且还能够提高服务器的安全性. 之前我们介绍 ...

  9. php环境搭建工具推荐

    楼楼最近由于一系列原因,使用了几款php环境搭建工具,安装配置方便,所以在这里推荐一下.第一款是XAMPP(网址http://www.xampps.com/),软件包原来的名字是 LAMPP,但是为了 ...

  10. SpringMvc 关于 EXCEL

    概述 我在使用SpingMvc 的EXCEL的发现传统的 AbstractJExcelView jexcel api已经过时 AbstractView poi Api 通过阅读官方文档发现建议我们使用 ...