Python 解LeetCode:Intersection of Two Arrays
最近,在用解决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的更多相关文章
- [LeetCode] Intersection of Two Arrays II 两个数组相交之二
Given two arrays, write a function to compute their intersection. Example:Given nums1 = [1, 2, 2, 1] ...
- [LeetCode] Intersection of Two Arrays 两个数组相交
Given two arrays, write a function to compute their intersection. Example:Given nums1 = [1, 2, 2, 1] ...
- LeetCode Intersection of Two Arrays
原题链接在这里:https://leetcode.com/problems/intersection-of-two-arrays/ 题目: Given two arrays, write a func ...
- LeetCode Intersection of Two Arrays II
原题链接在这里:https://leetcode.com/problems/intersection-of-two-arrays-ii/ 题目: Given two arrays, write a f ...
- [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 ...
- [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 ...
- 【leetcode❤python】350. Intersection of Two Arrays II
#-*- coding: UTF-8 -*- class Solution(object): def intersect(self, nums1, nums2): ...
- Python 解leetcode:48. Rotate Image
题目描述:把一个二维数组顺时针旋转90度: 思路: 对于数组每一圈进行旋转,使用m控制圈数: 每一圈的四个元素顺时针替换,可以直接使用Python的解包,使用k控制每一圈的具体元素: class So ...
- Python 解LeetCode:671. Second Minimum Node In a Binary Tree
题目在这里,要求一个二叉树的倒数第二个小的值.二叉树的特点是父节点的值会小于子节点的值,父节点要么没有子节点,要不左右孩子节点都有. 分析一下,根据定义,跟节点的值肯定是二叉树中最小的值,剩下的只需要 ...
随机推荐
- html表格宽度设置失效
问题描述: 我在写一个网页table时,table宽度超过了我预想的宽度,我想把它设置小一点,但总是没效果.改到怀疑人生!代码如下: 经过多次调试后发现一个问题,table可以改变大小,但是会有一个最 ...
- 【Windows 10 应用开发】细说文本资源文件(resw)
最近,小戏骨版<红楼梦>很是火热,老周一口气看完了9集,一直看到 Surface 的风扇呼呼响.林黛玉和薛宝钗这两个角色都演得不怎么样,倒是演史湘云的娃娃演得不错,老周甚是喜欢. 于是,昨 ...
- 【转】 Python subprocess模块学习总结
从Python 2.4开始,Python引入subprocess模块来管理子进程,以取代一些旧模块的方法:如 os.system.os.spawn*.os.popen*.popen2.*.comman ...
- UITextView实现限制100字
placeHoderLable = [[UILabel alloc]initWithFrame:CGRectMake(3, 3, DeviceWidth-6, 40)]; //根据情况调节位置 pla ...
- 当谈到 GitLab CI 的时候,我们都该聊些什么(下篇)
上篇主要介绍了 GitLab WorkFlow 以及 CI/CD 做的事情,并且详细分析 GitLab CI 跟 Runner 信息交互是如何进行的.接下来将为大家讲解 Executor 的实现,再通 ...
- fatal: The remote end hung up unexpectedly
git push 的时候出错,提示: fatal: The remote end hung up unexpectedly 遇见几次了,原因是因为文件太大,把限制放宽就好了.命令: git confi ...
- 简述static关键字、void与void *(void指针)、函数指针
static关键字1.修饰局部变量,延长局部变量的生命周期.使变量成为静态局部变量,在编译时就为变量分配内存,直到程序退出才释放存储单元.2.修饰全局变量,限制全局变量的使用范围为本文件中.全局变量默 ...
- Java中对图片文件的类型的获取
public static void main(String[] args) { File f = new File("c://test.jpg"); ...
- LINUX 笔记-命令执行顺序 && ,||
&& 格式:命令1 && 命令2 说明:命令1返回真(即返回0,成功被执行)后,命令2才能够被执行 例:/apps/bin目录将会被移到/apps/dev/bin目录下 ...
- ASP.NET没有魔法——ASP.NET 身份验证与Identity
前面的文章中为My Blog加入了文章的管理功能(ASP.NET没有魔法——ASP.NET MVC使用Area开发一个管理模块),但是管理功能应该只能由“作者”来访问,那么要如何控制用户的访问权限?也 ...