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 ...
随机推荐
- Asp.net MVC进入请求管道的过程
Asp.net MVC进入请求管道的过程 Asp.Net MVC 跟AspNet 入口解释 Asp.Net MVC请求处理过程 mvc 请求模型 mvc的原理 mvc模型 NewMVCPipleLin ...
- Linux C Programing - Arguments(2)
#include <iostream> #include <stdlib.h> #include <stdio.h> //printf #include <u ...
- 误删ibdata1文件恢复方法
注意:以下演示过程前提为mysqld进程仍在运行中,否则无法使用下面演示过程进行恢复! 1.手工制造故障,删除ibdata1文件,注意不要重启mysql shell > rm -rf ibdat ...
- 清除dns缓存
Linux清除dns缓存命令 /etc/init.d/dnsmasq restart
- 再谈Bellman-Ford
这几天学校女生节,挺累的,感觉还是挺好玩的,前几天看了一下最短路,Bellman-fort算法果然比较厉害,今天又参考了刘汝佳的两本书,有了一点新的认识. 废话不说,先上代码: #include &l ...
- javascript弹层组件
组件名称:layui 网址:http://layer.layui.com/ 里面有一个选择日期的组件,在导航的独立组件里,可以用来选择时间.
- asp.net core 通过 TeamCity 实现持续集成笔记
0x00 很早之前就想体验一把持续集成的快感,然后刚好手头上有个 asp.net core 的项目,就想来部署一下持续集成.一开始我是想用 Jenkins 的,弄了好半天,git 仓库没法同步下来,我 ...
- 分布式算法系列——一致性Hash算法
摘自:http://www.blogjava.net/hello-yun/archive/2012/10/10/389289.html
- 我的android学习经历38
anddroid studio的内存修改 昨天有位朋友问到了下面的一个问题 这个判断为android studio的分配的内存不够用. 据我的了解造成这个的原因主要有以下几个方面: 1.电脑的内存本来 ...
- Oracle的多表查询
多表查询概念: 所谓多表查询,又称表联合查询,即一条语句涉及到的表有多张,数据通过特定的连接进行联合显示. 基本语法: select column_name,.... from table1,tabl ...