LeetCode【第217题】Contains Duplicate
题目:
'''
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
''' '''
给定一个整数数组,找到该数组是否包含任何重复。 如果任何值在数组中至少出现两次,则函数应返回true,如果每个元素都不同,则函数应返回false。
'''
解法一55ms:
class Solution(object):
def containsDuplicate(self, nums):
"""
:type nums: List[int]
:rtype: bool
"""
if len(nums) == 0:
return False
else:
arr = {}
for i in range(len(nums)):
if nums[i] in arr:
return True
else:
arr[nums[i]] = i
else:
return False
解法二48ms:(一行)
class Solution(object):
def containsDuplicate(self, nums):
"""
:type nums: List[int]
:rtype: bool
"""
return len(nums) != len(set(nums))
LeetCode【第217题】Contains Duplicate的更多相关文章
- 【LeetCode】217 & 219 - Contains Duplicate & Contains Duplicate II
217 - Contains Duplicate Given an array of integers, find if the array contains any duplicates. You ...
- leetcode第217.题存在重复元素
1.题目描述 给定一个整数数组,判断是否存在重复元素. 如果任何值在数组中出现至少两次,函数返回 true.如果数组中每个元素都不相同,则返回 false. 2.示例 2.1 输入: [1,2,3,1 ...
- LeetCode(217)Contains Duplicate
题目 Given an array of integers, find if the array contains any duplicates. Your function should retur ...
- Leetcode算法刷题:217和219题 Contains Duplicate
从题目名字就可以看出这两道题是相似的,219是217的加强版 217:Contains Duplicate 题目 给予一个数组,判断是否有重复的元素.如果有就返回True,没有就返回False.以下是 ...
- LeetCode第[18]题(Java):4Sum 标签:Array
题目难度:Medium 题目: Given an array S of n integers, are there elements a, b, c, and d in S such that a + ...
- Leetcode第 217 场周赛(思维量比较大)
Leetcode第 217 场周赛 比赛链接:点这里 做完前两题我就知道今天的竞赛我已经结束了 这场比赛思维量还是比较大的. 1673. 找出最具竞争力的子序列 题目 给你一个整数数组 nums 和一 ...
- leetcode 第188题,我的解法,Best Time to Buy and Sell Stock IV
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255) ...
- leetcode第37题--Count and Say
题目:(据说是facebook的面试题哦) The count-and-say sequence is the sequence of integers beginning as follows:1, ...
- LeetCode第[1]题(Java):Two Sum 标签:Array
题目: Given an array of integers, return indices of the two numbers such that they add up to a specifi ...
- LeetCode的刷题利器(伪装到老板都无法diss你没有工作)
在工程效率大行其道的今天,如果不会写点代码以后也不容易在测试圈混下去.今天给大家推荐一个LeetCode的刷题利器,可以伪装到连你老板在这里走过去都无法确认你是在干活呢,还是在干活呢. LeetCod ...
随机推荐
- 自助用户选择VM Network
在VMM中为用户所属角色分配“作者VM网络”权限后,用户才可以在部署虚机的选择不同的VM Network,否则用户只能使用模板上所使用的VM Network,无法进行选择
- myeclipse中使用gradle开发项目
gradle可以直接使用maven的代码库,并且支持编程,可以说是maven的加强版.今天我们学习下,如何在MyEclipse下使用gradle开发项目.我们的开发环境:myeclipse 2015, ...
- cocos2dx 3.1创建工 mac
1.下载cocos2dx 3.1版本号 2.打开终端,cd 进入 cocos2d-x-3.1.1/tools/cocos2d-console/bin 3.cocos new game -p com.t ...
- Yum出错Error: Cannot find a valid baseurl for repo: base(转)
centos yum 错误 Error: Cannot find a valid baseurl for repo: base 装了个CentOS 6.x,使用yum时出现了下面的错误提示.Loade ...
- cocos2dx3.1.1+cocosstudio+lua问题总结
一.DeprecatedEnum.lua no value _G.LAYOUT_ABSOLUTE = ccui.Type.ABSOLUTE _G.LAYOUT_LINE ...
- 导出EXCEL数据时防止数值变科学计数的办法
网上有很多说法,最简单直接正确的做法是判断一下是否为数值以及长度,然后给单元格加上以下CSS即可: mso-generic-font-family:auto; mso-font-charset:1 ...
- Dynamic CRM:解决在创建业务流程时无法选择部分实体
在使用Dynamic CRM 业务流程的时候,会现在无法在步骤创建里选择我们想要的实体(如订单产品) 解决方法: 在SQL Server 中CRM数据库中找到[MetadataSchema].[Ent ...
- .net开发---自定义页面打印区域
自定义页面打印区域 有3种办法: 办法一:将不需要打印的部位隐藏掉 Examp: <%-- (1)使用css样式,定义一个.noprint的class,将不打印的内容放入这个class内. -- ...
- 在.Net中进行跨线程的控件操作(下篇:BackgroundWorker)
在.Net中,如果我们在非UI线程上访问窗体上的控件的时候,会产生一个跨线程调用的异常,那么如何处理这种情况呢?在上一章中,我介绍了使用Control.Invoke方法,如果你不习惯使用委托,那么.N ...
- mssql死锁问题
在网上查看了很多死锁与阻塞的资料,为什么会出现死锁或者阻塞? 阻塞在大数据量的数据库中经常出现,在我现在的其中一个项目出现的频率很高,根据网上查到死锁跟阻塞的资料,当时分析出来,主要是多台设备同时调用 ...