当前位置:网站首页>[LeetCode] 存在重复元素【217】
[LeetCode] 存在重复元素【217】
2022-07-02 22:06:00 【山茶花开时。】
问题: 给你一个整数数组nums,如果任一值在数组中出现至少两次,返回True;如果数组中每个元素互不相同,返回False
示例1
输入: nums = [1,2,3,1]
输出: True
示例2
输入: nums = [1,2,3,4]
输出: False
示例3
输入: nums = [1,1,1,3,3,4,3,2,4,2]
输出: True
Python3解题
# 解法1:set去重,比较长度
def containsDuplicate(nums):
len_set_nums = len(set(nums))
if (len_set_nums != len(nums)):
return True
else:
return False
# 解法2:排序,比较是否相等
def containsDuplicate(nums):
nums.sort()
for i in range(len(nums)-1):
if nums[i+1] == nums[i]:
return True
return False
# 解法3
def containsDuplicate(nums):
if len(nums) <= 1:
return False
hashset = set()
for i in nums:
if i in hashset:
return True
else:
hashset.add(i)
return False
containsDuplicate([1,2,3,1]) # True
containsDuplicate([1,2,3,4]) # False
containsDuplicate([1,1,1,3,3,4,3,2,4,2]) # True
边栏推荐
- SimpleITK使用——4. 奇怪的問題
- Market Research - current situation and future development trend of environmental friendly fireworks Market
- Basic concepts of image and deep understanding of yuv/rgb
- 【AUTOSAR-DCM】-4.3-UDS $22和$2E服务如何读取和写入NVM数据
- Notes on key vocabulary in the English original of the biography of jobs (10) [chapter eight]
- 《乔布斯传》英文原著重点词汇笔记(九)【 chapter seven】
- Socket套接字C/S端流程
- Necessary browser plug-ins for network security engineers
- [shutter] shutter opens a third-party application (url|launcher plug-in search and installation | url| launcher plug-in official example | open browser | open a third-party application)
- JS获取display为none的隐藏元素的宽度和高度的解决方案
猜你喜欢
"Actbert" Baidu & Sydney University of technology proposed actbert to learn the global and local video text representation, which is effective in five video text tasks!
kubernetes 使用主机名将 pod 分配在指定节点上
New feature of go1.18: trylock, which has been tossed n times
540. Single element in ordered array
Phpcms realizes the direct Alipay payment function of orders
NC50965 Largest Rectangle in a Histogram
Hanoi Tower problem
大话云原生之负载均衡篇-小饭馆客流量变大了
任务和特权级保护
[shutter] shutter application theme (themedata | dynamic modification theme)
随机推荐
Hanoi Tower problem
【ODX Studio编辑PDX】-0.1-如何快速查看各Variant变体间的支持的诊断信息差异(服务,Sub-Function...)
PHP implements querying the data matching the date of birth according to the entered age
phpcms实现订单直接支付宝支付功能
Market Research - current situation and future development trend of sickle cell therapy Market
Web side defense Guide
Utilisation de simpletk - 4. Question étrange
NC24325 [USACO 2012 Mar S]Flowerpot
Necessary browser plug-ins for network security engineers
How should programmers write logs
影视随摘
Based on asp Net (used mobile phone sales management system) +asp Net+c # language +vs2010+ database can be used for course design and post design learning
Developers share | HLS and skillfully use Axi_ Customize the master bus interface instructions and improve the data bandwidth - area exchange speed
PHP微信抢红包的算法
#include errors detected. Please update your includePath.
App page sharing password rails implementation
Market Research - current situation and future development trend of preclinical medical device testing service market
SimpleITK使用——4. 奇怪的问题
[micro service sentinel] rewrite Sentinel's interface blockexceptionhandler
基于ASP.net的手机销售管理系统(二手手机销售管理系统)+ASP.NET+C#语言+VS2010+数据库可以用于课设、毕设学习