python leetcode 日记 --Contains Duplicate --217
题目
Given an array of integers, find if the array contains any duplicates. Your function should return true if any value appears at least twice in the array, and it should return false if every element is distinct.
Subscribe to see which companies asked this question
输入输出如下:
"""
:type nums: List[int]
:rtype: bool
"""
浏览题目,如果遍历每一个元素,不可避免的复杂度将达到o(n2)级别,因此采用字典进行,没读一个数将其保存在字典当中
def containsDuplicate(self, nums):
dic={}
for i in nums:
if dic.get(i)==None:
dic[i]=1
else:
return True
return False
至此便可完成。
之后浏览大神解法,发现可以利用set,判断前后的长度是否一致即可
class Solution(object):
def containsDuplicate(self, nums):
"""
:type nums: List[int]
:rtype: bool
"""
return len(nums) != len(set(nums))
引自https://leetcode.com/discuss/67164/one-line-solution-in-python
python leetcode 日记 --Contains Duplicate --217的更多相关文章
- python leetcode 日记 --Contains Duplicate II --219
题目: Given an array of integers and an integer k, find out whether there are two distinct indices i a ...
- python leetcode 日记--Maximal Square--221
题目: Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and ...
- python leetcode 日记--231. Power of Two
题目: Given an integer, write a function to determine if it is a power of two. class Solution(object): ...
- [LeetCode] 219. Contains Duplicate II 包含重复元素 II
Given an array of integers and an integer k, find out whether there are two distinct indices i and j ...
- [LeetCode] 220. Contains Duplicate III 包含重复元素 III
Given an array of integers, find out whether there are two distinct indices i and j in the array suc ...
- 2017/11/3 Leetcode 日记
2017/11/3 Leetcode 日记 654. Maximum Binary Tree Given an integer array with no duplicates. A maximum ...
- Python 学习日记(第三周)
知识回顾 在上一周的学习里,我学习了一些学习Python的基础知识下面先简短的回顾一些: 1Python的版本和和安装 Python的版本主要有2.x和3.x两个版本这两个版本在语法等方面有一定的区别 ...
- Python学习日记 --day2
Python学习日记 --day2 1.格式化输出:% s d (%为占位符 s为字符串类型 d为数字类型) name = input('请输入姓名') age = int(input('请输入年龄 ...
- python学习日记(基础数据类型及其方法01)
数字 int 主要是用于计算的,常用的方法有一种 #既十进制数值用二进制表示时,最少使用的位数i = 3#3的ASCII为:0000 0011,即两位 s = i.bit_length() print ...
随机推荐
- php curl的使用
我们来采集一个页面,通常情况下,我们会使用file_get_contents()函数来获取: <?php $str = file_get_contents('http://www.baidu.c ...
- 使用cygwin出现syntax error near unexpected token'$'do\r
直接从csdn复制粘贴的.sh代码,放到cygwin下运行sh的时候出错syntax error near unexpected token'$'do\r 解决方法: 1.下载notepad++ 2. ...
- [课程设计]Scrum 2.7 多鱼点餐系统开发进度(下单一览页面-菜式添加功能的继续实现)
Scrum 2.7 多鱼点餐系统开发进度 (下单一览页面-菜式添加功能的继续实现) 1.团队名称:重案组 2.团队目标:长期经营,积累客户充分准备,伺机而行 3.团队口号:矢志不渝,追求完美 4.团 ...
- noi 4977 怪盗基德的滑翔翼
题目链接: http://noi.openjudge.cn/ch0206/4977/ LIS http://paste.ubuntu.com/23406594/
- 用jxl解析excel内容
需要导入jxl.jar 下方表格为excel中内容: 序号 姓名 性别 生日 地址 1 测试1 男 1990-1-1 北京朝阳区 2 测试2 女 1998-2-2 北京海淀 3 测试3 男 1999- ...
- 富文本常用封装(NSAttributedString浅析)
最近经常遇到关于富文本的一些需求,特此封装了几个最常用的API分享给大家,但授之以鱼不如授之以渔,接下来会顺便谈谈NSAttributedString,确保你读了本篇文章能够自己封装关于富文本的API ...
- Spring 框架下Controller 返回结果在EasyUI显示
这几天弄了一下java下的在后台返回数据到jsp页面上的显示: 总结一下: 首先后台方面: @RequestMapping(value="/searchByUserName") @ ...
- 用cython提升python的性能
Boosting performance with Cython Even with my old pc (AMD Athlon II, 3GB ram), I seldom run into ...
- Beautiful 疑问小记
一.获取id和class的text() html = urlopen(real_url) bsObj = BeautifulSoup(html) h1 = bsObj.h1.get_text() co ...
- switch多分支语句简析
在编程中一个常见问题就是检测一个变量是否符合某个条件,switch以一个简单明了的方式来实现类似于"多选一"的选择,语法格式如下: /*switch首先计算表达式的值,如果表达式的 ...