当前位置:网站首页>[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边栏推荐
- Basic concepts of image and deep understanding of yuv/rgb
- Simpleitk use - 4 Strange question
- 百度智能云-创建人脸识别应用
- 杰理之修改不需要长按开机功能【篇】
- Try to get property'num for PHP database data reading_ rows' of non-object?
- Oracle-游标
- Share how to make professional hand drawn electronic maps
- Ransack combined condition search implementation
- What is the'function'keyword used in some bash scripts- What is the 'function' keyword used in some bash scripts?
- Scrcpy this software solves the problem of sharing mobile screen with colleagues | community essay solicitation
猜你喜欢

图形视图框架

UE4 游戏架构 学习笔记

Oracle-PL/SQL编程

Socket套接字C/S端流程

Micro service gateway selection, please accept my knees!

Kubernetes resource object introduction and common commands (4)

牛客网:最大子矩阵

540. Single element in ordered array

NC50965 Largest Rectangle in a Histogram

Source code analysis - lightweight asynchronous crawler framework Ruia
随机推荐
Service visibility and observability
Pointer array parameter passing, pointer parameter passing
Struct, bit segment, enumeration, union
[micro service sentinel] rewrite Sentinel's interface blockexceptionhandler
`${}`的用法
Task and privilege level protection
PHP微信抢红包的算法
PHP implements querying the data matching the date of birth according to the entered age
Attack and defense world PWN question: Echo
wait解决僵尸进程
杰理之内置短按再长按,不管长按多长时间都是短按【篇】
杰理之如何测试按键的误触率【篇】
UE4 UI自适应屏幕
Commodity information management system (C language document version)
Source code analysis - lightweight asynchronous crawler framework Ruia
杰理之样机无触摸,拆机之后重新安装变正常【篇】
Pointer - function pointer
Market Research - current market situation and future development trend of genome editing mutation detection kit
SimpleITK使用——4. 奇怪的问题
Market Research - current market situation and future development trend of aircraft wireless intercom system