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.

原题地址: Contains Duplicate

难度: Easy

题意: 判断数组中是否存在重复的数

思路1:

排序, 遍历数组,比较前后两个数是否相等

class Solution(object):
def containsDuplicate(self, nums):
"""
:type nums: List[int]
:rtype: bool
"""
nums.sort()
for i in range(len(nums)-1):
if nums[i] == nums[i+1]:
return True
return False

时间复杂度:O(nlog(n))

空间复杂度:O(1)

思路2:

统计每个数的个数

class Solution(object):
def containsDuplicate(self, nums):
"""
:type nums: List[int]
:rtype: bool
"""
d = {}
     for num in nums:
      d[num] = d.get(num, 0) + 1
       if d[num] > 1:
return True
return False

时间复杂度:O(n)

空间复杂度:O(n)

思路3:

转化为set(),比较list和set的长度,相等说明没有重复的数,不相等说明存在重复的数

class Solution(object):
def containsDuplicate(self, nums):
"""
:type nums: List[int]
:rtype: bool
"""
return len(nums) == len(set(nums))

时间复杂度:O(n)

空间复杂度:O(n)

217. Contains Duplicate@python的更多相关文章

  1. 217. Contains Duplicate(C++)

    217. Contains Duplicate Given an array of integers, find if the array contains any duplicates. Your ...

  2. 25. leetcode 217. Contains Duplicate

    217. Contains Duplicate Given an array of integers, find if the array contains any duplicates. Your ...

  3. LeetCode Javascript实现 169. Majority Element 217. Contains Duplicate(两个对象比较是否相等时,如果都指向同一个对象,a==b才是true)350. Intersection of Two Arrays II

    169. Majority Element /** * @param {number[]} nums * @return {number} */ var majorityElement = funct ...

  4. leetcode 217. Contains Duplicate 287. Find the Duplicate Number 442. Find All Duplicates in an Array 448. Find All Numbers Disappeared in an Array

    后面3个题都是限制在1-n的,所有可以不先排序,可以利用巧方法做.最后两个题几乎一模一样. 217. Contains Duplicate class Solution { public: bool ...

  5. 217. Contains Duplicate【easy】

    217. Contains Duplicate[easy] Given an array of integers, find if the array contains any duplicates. ...

  6. LN : leetcode 217 Contains Duplicate

    lc 217 Contains Duplicate 217 Contains Duplicate Given an array of integers, find if the array conta ...

  7. 【LeetCode】217. Contains Duplicate 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典统计词频 使用set 排序 日期 [LeetCo ...

  8. 【leetcode❤python】217. Contains Duplicate

    #-*- coding: UTF-8 -*- class Solution(object):    def containsDuplicate(self, nums):        numsdic= ...

  9. [LeetCode&Python] Problem 217. Contains Duplicate

    Given an array of integers, find if the array contains any duplicates. Your function should return t ...

随机推荐

  1. C++笔试题(十)

    这些题目相比其他公司的试题,较为基础,全部为C语言,没有涉及C++,但如果不细心,是很难得到较高分数的.另外大家转贴不要去掉我的个人信息啊.互相宣传下网站嘛.1. 找错 void test1() { ...

  2. SQLite3初级使用

    (1)SQL的指令格式 所有的SQL指令都是以分号(;)结尾的.如果遇到两个减号(--)则代表注解,sqlite3会略过去. (2)建立资料表 假设我们要建一个名叫film的资料表,只要键入以下指令就 ...

  3. sybase修改默认字符集为cp936

    原文地址:http://blog.sina.com.cn/s/blog_4d6854860100xn3f.html 报错信息:2402 error converting characters into ...

  4. 阻止默认行为是配合passive使用

    在使用lighthouse检测pwa应用时,发现提示下面有下面的警告 默认使用passive:true提高滚动性能并减少崩溃,passive即顺从的,是指它顺从浏览器的默认行为.设置该属性的目的主要是 ...

  5. 真tm无聊,这几天。。。

    临近期末了,每天都要和学霸一起上自习. 很不喜欢学习和自己未来没多大用的东西 老师画的那些重点是对我们好吗~ 每天感觉都在折磨自己,不想学,学不进去,心里很烦躁,浮躁. 人生苦短->_-> ...

  6. poj2455Secret Milking Machine(二分+最大流)

    链接 二分距离,小于当前距离的边容量+1,使最后流>=t 注意 会有重边 #include <iostream> #include<cstdio> #include< ...

  7. P1320 压缩技术(续集版)

    题目描述 设某汉字由N X N的0和1的点阵图案组成,如下图.我们依照以下规则生成压缩码.连续一组数值:从汉字点阵图案的第一行第一个符号开始计算,按书写顺序从上到下,由左到右.第一个数表示连续有几个0 ...

  8. idea DeBug调试学习

    在Intellij IDEA中使用Debug 目录 一.Debug开篇 二.基本用法&快捷键 三.变量查看 四.计算表达式 五.智能步入 六.断点条件设置 七.多线程调试 八.回退断点 九.中 ...

  9. AJPFX谈JAVA新手问题之异常处理使用不当

    ★空的 catch 语句块 犯这种错误的人比较少,一般发生在刚学会 Java 或者刚参加工作不久的人身上. 所谓“空 catch 语句块”就是在 catch 语句块中没有对异常作任何处理(比如记错误日 ...

  10. [ Luogu 3709 ] 大爷的字符串题

    \(\\\) Description 原题题面太过混乱出题人语文凉凉 给出一个长为 \(n\) 的数列 \(A\) ,多次询问: 对于一个区间 \([L_i,R_i]\),把区间内的所有数最少划分成多 ...