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 ...
随机推荐
- Prince2的七大原则(7)
[Prince2科普]Prince2的七大原则(7) 2016-12-12 光环组织级项目管理 按照惯例我们先来回顾一下,PRINCE2七大原则分别是指:持续的业务验证,经验学习,角色与责任,按阶段管 ...
- (转) Eclipse连接MySQL数据库(傻瓜篇)
Eclipse连接MySQL数据库(傻瓜篇) 原帖地址: http://www.cnblogs.com/fnng/archive/2011/07/18/2110023.html Posted on 2 ...
- Viking Village维京村落demo中的地面积水效果
效果如下: 似乎是通过高光贴图实现的,查找后发现具体在这: 它使用了基于Standard的TerrainSurface自定义Shader,关闭该帖图后效果消失: 这个TerrainSurfaceSha ...
- C# 4.0中dynamic的作用
internal sealed class Coffee { public string GetName() { return "You selected Maxwell coffee.&q ...
- Annotation
Annotation是给类,方法或域上加的一种特殊的标记,可以通过反射取到注解的类型和值,从而完成某种特定的操作. 定义注解需要使用元注解,元注解有@Retention和@Target p.p1 { ...
- Hibernate的关联映射——单向1-1关联
Hibernate的关联映射--单向1-1关联 对于单向的1-1关联关系,需要在持久化类里增加代表关联实体的成员变量,并为该成员变量添加setter方法和getter方法.从持久化类的代码上看,单向1 ...
- centos7 docker mysql56
yum -y install docker docker pull centos docker run --name=mysqltmp -i -t centos /bin/bash yum -y in ...
- hosts代理
hosts代理文件:C:\Windows\System32\drivers\etc\HOSTS 内容如下: # Copyright (c) -, racaljk. # https://github.c ...
- "SQLServer无法打开用户默认数据库,登录失败,错误4064"的解决办法
"SQLServer无法打开用户默认数据库,登录失败,错误4064"的解决办法 1.检查登录密码 如果密码错误,修改数据库密码,用windows身份验证登录进去, (1)安全--登 ...
- ITerm2下使用ssh访问Linux
通常情况下,iTerm2访问远程Linux使用ssh,与Termial基本一样,方法如下: ssh <用户名>@<ip> 然后输入访问的密码即可.当然还有的时候需要指定访问端口 ...