题目

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的更多相关文章

  1. 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 ...

  2. 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 ...

  3. python leetcode 日记--231. Power of Two

    题目: Given an integer, write a function to determine if it is a power of two. class Solution(object): ...

  4. [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 ...

  5. [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 ...

  6. 2017/11/3 Leetcode 日记

    2017/11/3 Leetcode 日记 654. Maximum Binary Tree Given an integer array with no duplicates. A maximum ...

  7. Python 学习日记(第三周)

    知识回顾 在上一周的学习里,我学习了一些学习Python的基础知识下面先简短的回顾一些: 1Python的版本和和安装 Python的版本主要有2.x和3.x两个版本这两个版本在语法等方面有一定的区别 ...

  8. Python学习日记 --day2

    Python学习日记 --day2 1.格式化输出:% s d  (%为占位符 s为字符串类型 d为数字类型) name = input('请输入姓名') age = int(input('请输入年龄 ...

  9. python学习日记(基础数据类型及其方法01)

    数字 int 主要是用于计算的,常用的方法有一种 #既十进制数值用二进制表示时,最少使用的位数i = 3#3的ASCII为:0000 0011,即两位 s = i.bit_length() print ...

随机推荐

  1. 处理SecureCRT中使用vim出现中文乱码问题

    处理SecureCRT中使用vim出现中文乱码问题 引用原文:http://blog.chinaunix.net/uid-20639775-id-3475608.html因为cat没有问题,定位是vi ...

  2. Writing Your Own jQuery Plugins

    Setting Up <script src="js/jquery-1.9.1.min.js"></script> <script src=" ...

  3. MVC5+EF6 入门完整教程11--细说MVC中仓储模式的应用

    摘要: 第一阶段1~10篇已经覆盖了MVC开发必要的基本知识. 第二阶段11-20篇将会侧重于专题的讲解,一篇文章解决一个实际问题. 根据园友的反馈, 本篇文章将会先对呼声最高的仓储模式进行讲解. 文 ...

  4. Java 新手学习 CSS样式列表 排版 格式布局

    1,样式表分为  内联样式表   内嵌样式表  外部样式表  三种. 内联样式表是直接写在标签里面的  比如 <p style=“”></p>  <div style=& ...

  5. 【前端】Web前端学习笔记【1】

    ... [2015.12.02-2016.02.22]期间的学习笔记. 相关博客: Web前端学习笔记[2] 1. JS中的: (1)continue 语句 (带有或不带标签引用)只能用在循环中. ( ...

  6. [问题2014A10] 解答

    [问题2014A10]  解答 考虑如下变形: \[(I_n-A)^2=(AA'-A)(I_n-A)=A(A'-I_n)(I_n-A)=-A(I_n-A)'(I_n-A).\] 因为 \(A\) 是非 ...

  7. Intellij自动下载导入框架包

    忽然发现intellij尽然可以自动导入 框架所需的包,而且可以选择jar包版本,瞬间发现Maven,gradle管理jar包还得写配置文件弱爆了. 以Hibernate为例: 1.ProjectSt ...

  8. js函数式编程

    最近在看朴灵的<深入浅出nodejs>其中讲到函数式编程.理解记录下 高阶函数 比较常见,即将函数作为参数,或是将函数作为返回值得函数. 如ECMAScript5中提供的一些数组方法 fo ...

  9. PHP 小方法之 算生日

    if (! function_exists ( 'diff_date' )) { function diff_date($date1, $date2){ $datestart = date ( 'Y- ...

  10. 深入理解HTTP协议、HTTP协议原理分析

    http://blog.csdn.net/g1036583997/article/details/50457441