217. Contains Duplicate@python
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的更多相关文章
- 217. Contains Duplicate(C++)
217. Contains Duplicate Given an array of integers, find if the array contains any duplicates. Your ...
- 25. leetcode 217. Contains Duplicate
217. Contains Duplicate Given an array of integers, find if the array contains any duplicates. Your ...
- 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 ...
- 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 ...
- 217. Contains Duplicate【easy】
217. Contains Duplicate[easy] Given an array of integers, find if the array contains any duplicates. ...
- LN : leetcode 217 Contains Duplicate
lc 217 Contains Duplicate 217 Contains Duplicate Given an array of integers, find if the array conta ...
- 【LeetCode】217. Contains Duplicate 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典统计词频 使用set 排序 日期 [LeetCo ...
- 【leetcode❤python】217. Contains Duplicate
#-*- coding: UTF-8 -*- class Solution(object): def containsDuplicate(self, nums): numsdic= ...
- [LeetCode&Python] Problem 217. Contains Duplicate
Given an array of integers, find if the array contains any duplicates. Your function should return t ...
随机推荐
- PhpStorm之设置字体大小
1.点击左上角的File,再点击setting:(Ctrl+Alt+S) 2.进入 Editor / General,选择 Change font size (Zoom) with Ctrl+Mo ...
- 51nod 1247 可能的路径(gcd)
传送门 题意 略 分析 有以下结论 \(1.(x,y)->(y,x)\) \(2.(x,y)->(a,b)==>(a,b)->(x,y)\) 证明 做如下变换 \((a,b)- ...
- hdoj5301
题意: 有一个n*m的大矩阵, 其中有一个1*1的不要的位置(x,y), 然后用若干个小矩阵去覆盖大矩阵, 不要的不能被覆盖. 问小矩阵中面积最大的面积最小是多少. 思路: 巨巨先画一个矩形,看看那个 ...
- JVM的参数详解
今天就整理下JVM的参数!相对而已JVM有很多参数,但对于一般开发人员执行知道和堆栈大小,GC.远程调试的参数即可. JVM Parameters 首先需要说明的是JVM的参数形式: -X 开头的参数 ...
- shell Syntax error: Bad fd number 错误解决
最近在玩spark , 需要看一下python的spark lib 是怎么加入环境变量的. 执行: sh -x bin/pyspark 报错 + dirname bin/pyspark + cd bi ...
- 黑客攻防技术宝典Web实战篇:避开客户端控件习题
猫宁!!! 参考链接:http://www.ituring.com.cn/book/885 随书答案. 1. 通过客户端传送的数据如何阻止破坏性攻击? 可以使用保存在服务器上的密钥对数据进行加密或散列 ...
- 第二篇 .NET高级技术之密闭类和静态类及扩展方法
1.密闭类是修饰为sealed的类, sealed不能有子类.一般只有系统中的一些基本类声明为sealed.面试题:是否可以编写一个类继承自String类? 答:不能,因为string被声明为了sea ...
- vim 不同的插入方式
在命令行模式下进入到输入模式 可以敲击 i.a.o. s. I. A. O. S 它们之间的区别做下备忘:i:在光标所在字符前开始插入a:在光标所在字符后开始插入o:在光标所在行的下面另起一新行插入s ...
- 华容道 noip2013 70分搜索
题目描述 [问题描述] 小 B 最近迷上了华容道,可是他总是要花很长的时间才能完成一次.于是,他想到用编程来完成华容道:给定一种局面, 华容道是否根本就无法完成,如果能完成, 最少需要多少时间. 小 ...
- 解决vue跨域问题
package com.qmtt.config; import java.io.IOException; import javax.servlet.Filter; import javax.servl ...