leetcode之Find the Duplicate Number
来源:互联网 发布:淘宝代码模板 编辑:程序博客网 时间:2024/04/28 06:41
之前很早就看到这个题了,但是被O(1)的空间复杂度以及小于O(n**2)的时间复杂度难住了,结果今天在查另一个问题的时候偶然发现了有类似内置的函数来解决问题。from
collections
import
Counter,collections里的Counter函数可以直接统计出来各个元素的数量,虽然是Counter类型,但是可以用字典的相关函数来解决。于是本道题就圆满解决了。估计时间复杂度可以考虑看源码来解决了。代码如下:
def findDuplicate(nums):""" :type nums: List[int] :rtype: int """from collections import Countera = Counter(nums)for i in a.keys():if a[i] != 1:return i其实可以做个字典,看见一个数字就来问一下有没有这个key,有了自然是重复了,没有就填进去
0 0
- leetcode之Find the Duplicate Number
- leetcode之Find the Duplicate Number
- leetcode之Find the Duplicate Number 问题
- leetcode Find the Duplicate Number
- *LeetCode-Find the Duplicate Number
- [Leetcode] Find the Duplicate Number
- Leetcode -- Find the Duplicate Number
- LeetCode----Find the Duplicate Number
- LeetCode Find the Duplicate Number
- LeetCode -- Find the Duplicate Number
- [Leetcode]Find the Duplicate Number
- leetcode Find the Duplicate Number
- 【Leetcode】Find the Duplicate Number
- LeetCode Find the Duplicate Number
- Leetcode: Find the Duplicate Number
- leetcode: Find the Duplicate Number
- leetcode--Find the Duplicate Number
- 《leetCode》:Find the Duplicate Number
- 高精度求余-HDOJ-5373-The Shortest Problem
- Android获取到inflate服务的方式及inflate的解析过程
- linux Centos下Mplayer没有声音的解决方法
- cadence学习笔记之生成网表错误
- 错误记录]UITableView删除行的问题
- leetcode之Find the Duplicate Number
- angular.copy
- Lua的require使用
- 拷贝构造函数调用时机
- WordPress中wp-Syntax插件使用方法
- block回调的一个小例子
- BestCoder Round #59 (div.2)A.SDOI
- DP-HDOJ-5400-Arithmetic Sequence
- startActivityForResult用法