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 ...
随机推荐
- e_pro list 1
uncle n. 叔叔; 伯父; 舅父; 姑父,姨父; farm n. 农家; 农田; 农场,农庄; 畜牧场; corner n. 拐角; 角落,角; 困境; [商] 囤积; vi. 驾车转 ...
- Codeforces Round #357 (Div. 2)C. Heap Operations
用单调队列(从小到大),模拟一下就好了,主要是getMin比较麻烦,算了,都是模拟....也没什么好说的.. #include<cstdio> #include<map> #i ...
- 洛谷 P4125 [WC2012]记忆中的水杉树【扫描线+set+线段树】
我没有找到能在bzojAC的代码--当然我也WA了--但是我在洛谷过了,那就假装过了吧 minmax线段树一开始写的只能用min更新min,max更新max,实际上是可以互相更新的-- 首先看第二问, ...
- bzoj 1982: [Spoj 2021]Moving Pebbles【博弈论】
必败状态是n为偶数并且数量相同的石子堆可以两两配对,因为这样后手可以模仿先手操作 其他状态一定可以由先手给后手一步拼出一个必败状态(用最大堆补) #include<iostream> #i ...
- 洛谷P4867 Gty的二逼妹子序列(莫队+树状数组)
传送门 本来打算用主席树 然后发现没办法维护颜色数 于是用了莫队加树状数组 然后竟然A了…… //minamoto #include<iostream> #include<cstdi ...
- MySQL 错误码对照
1005:创建表失败 1006:创建数据库失败 1007:数据库已存在,创建数据库失败 1008:数据库不存在,删除数据库失败 1009:不能删除数据库文件导致删除数据库失败 1010:不能删除数据目 ...
- java hashCode 作用
hashCode 作用,对象根据hashCode的值分区域存放 /** * hashCode 作用 * * @author Administrator * */ public class Point ...
- Web | 解决中文乱码
设定文件的编码格式在head中添加 <head> <meta http-equiv="Content-Type" content="text/html; ...
- Github配置SSH连接
安装git.exe,打开Git Bash 1.检查是否已经有SSH Key. $cd /.ssh 2.生成一个新的SSH. $ ssh-keygen -t rsa -C "email@git ...
- 洛谷 P3327 [SDOI2015]约数个数和 || Number Challenge Codeforces - 235E
https://www.luogu.org/problemnew/show/P3327 不会做. 去搜题解...为什么题解都用了一个奇怪的公式?太奇怪了啊... 公式是这样的: $d(xy)=\sum ...