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 ...
随机推荐
- 修改yv12像素值
YV12内存布局 1 unsigned char *pY = (unsigned char *)pBuf; unsigned char *pV = pY + nWidth * nHeight; uns ...
- ASP.NET 管道事件与HttpModule, HttpHandler简单理解
BeginRequest 指示请求处理开始 AuthenticateRequest 封装请求身份验证过程 AuthorizeRequest 封装检查是否能利用以前缓存的输出页面处理请求的过程 Reso ...
- find函数
Python find() 方法检测字符串中是否包含子字符串 str ,如果指定 beg(开始) 和 end(结束) 范围,则检查是否包含在指定范围内,如果包含子字符串返回开始的索引值,否则返回-1. ...
- EXCEL计算数字、汉字、英文单元格的计数
1.数字COUNT(A1:A100)2.汉字{=SUMPRODUCT(IF(LEN(A1:A100)LENB(A1:A100),1,0)*1)}3.英文{=SUMPRODUCT(IF(ISTEXT(A ...
- linux应用程序开发-文件编程-库函数
---恢复内容开始--- c库函数文件操作独立于具体的系统平台,可移植性较好. 库函数-创建和打开 FILE*fopen(const char*filename,const char*mode) mo ...
- charles抓包的使用教程
// Charles 从入门到精通:(http://blog.csdn.net/donwei8/article/details/51647752) //抓包工具:Charles-断点修改 教程 (ht ...
- 转:C++的重载(overload)与重写(override)
C++ override overload 的区别 override是指在不同作用域中,多个函数原型完全一样,而实现却不同的函数.在C++中,经常发生在类的继承中.当基类中的某个方法是virtual ...
- 常用sql语句总结
一.连接数据库 mysql -h127.0.0.1(连接地址) -P3303(端口号) -uroot(用户名) -p123456(密码) 二.常用语句 show datebases; 查看数据库 cr ...
- Gradle version 2.2 is required. Current version is 2.14.1.
gradle版本错误: 1. 修改gradle\wrapper\gradle-wrapper.properties文件: distributionUrl=https\://services.gradl ...
- HBase之计数器
HBase计数器 #创建counters表 列族['daily','weekly','monthly'] hbase(main):001:0> create 'counters','daily' ...