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 ...
随机推荐
- Hibernate关联关系映射
1. Hibernate关联关系映射 1.1. one to one <class name="Person"> <id name="id" ...
- centos 5.4 安装nodejs + npm(转)
而在安装nodejs的时候,需要用到,所以需要手动安装bz2库. sudo yum install -y bzip2* cd Python-/Modules/zlib ./configure make ...
- bash shell for循环1到100 .
前言 用bash shell写程序时,经常会用到for循环,特别是从1到100这种需求,这里记录几种shell中从1到100的循环方法 方法 类c语言 for ((i=1; i<=100; ...
- 整理 iOS 9 适配中出现的坑
本文主要是说一些iOS9适配中出现的坑,如果只是要单纯的了解iOS9新特性可以看瞄神的开发者所需要知道的 iOS 9 SDK 新特性.9月17日凌晨,苹果给用户推送了iOS9正式版,随着有用户陆续升级 ...
- Android(java)学习笔记152:Android运行时异常“Binary XML file line # : Error inflating class”
在原生Android下编译APK,编译没有问题,但是在运行的时候经常出现如标题所描述的异常:"Binary XML file line # : Error inflating class&q ...
- 需要设置jdk的三处位置:
需要设置jdk的三处位置:1.tomcat需要一个JDK : Windows--->Preferences--->MyEclipse--->Servers--->Tomcat- ...
- js相差天数计算
dueDate = new Date(row.dueDate.replace(/-/g,'/')); if(dueDate<systemDate){ ...
- CentOS 6.3 安装ATI显卡驱动
环境: centos 6.3 内核(GNU貌似大家都知道) 显卡:ATI Mobility Radeon HD 3470 安装显卡流程: 1.官网下载最新驱动 http://support.amd. ...
- 关于FPGA异步时钟采样--结绳法的点点滴滴
一.典型方法 典型方法即双锁存器法,第一个锁存器可能出现亚稳态,但是第二个锁存器出现亚稳态的几率已经降到非常小,双锁存器虽然不能完全根除亚稳态的出现(事实上所有电路都无法根除,只能尽可能降低亚稳态的出 ...
- 学习笔记_JDBC_1_Demo1_连接数据库的基本操作和步骤
常见错误:连数据库时,这句话String url = "jdbc:mysql://localhost/数据库名";数据库名可能和你建的数据库名不一样 1.安装Mysql,记住此时你 ...