当前位置:网站首页>[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边栏推荐
- Learn computer knowledge from scratch
- Developers share | HLS and skillfully use Axi_ Customize the master bus interface instructions and improve the data bandwidth - area exchange speed
- 基于ASP.net的手机销售管理系统(二手手机销售管理系统)+ASP.NET+C#语言+VS2010+数据库可以用于课设、毕设学习
- 杰理之样机在多次触摸后会触发关机【篇】
- Build your own website (22)
- [shutter] shutter gesture interaction (small ball following the movement of fingers)
- NC24325 [USACO 2012 Mar S]Flowerpot
- 杰理之充电拔出,无法触摸开机【篇】
- [shutter] shutter page life cycle (initialization period | createstate | initstate | update period | build | destroy period | dispose)
- NC50965 Largest Rectangle in a Histogram
猜你喜欢

Objects and object variables

PMP项目整合管理

#include errors detected. Please update your includePath.

建立自己的网站(22)

开发者分享 | HLS, 巧用AXI_master总线接口指令的定制并提升数据带宽-面积换速度...

Lightgbm principle and its application in astronomical data

Source code analysis - lightweight asynchronous crawler framework Ruia

数学建模——图与网络模型及方法(一)

Struct, bit segment, enumeration, union

附加:【登录信息存储】与【登录状态校验】;(包括:总结了到目前为止,有关【登录信息存储】与【登录状态校验】的所有内容;)
随机推荐
PHP微信抢红包的算法
Technological Entrepreneurship: failure is not success, but reflection is
U++ 学习笔记 堆
Market Research - current situation and future development trend of marine clutch Market
[QT] Q multithreaded development - Analysis of multithreaded application examples (Mandelbrot)
杰理之样机无触摸,拆机之后重新安装变正常【篇】
Sql service intercepts string
Market Research - current situation and future development trend of environmental friendly fireworks Market
Try to get property'num for PHP database data reading_ rows' of non-object?
How can I use knockout's $parent/$root pseudovariables from inside a . computed() observable?
#include errors detected. Please update your includePath.
#include errors detected. Please update your includePath.
Market Research - current situation and future development trend of preclinical medical device testing service market
Basic concepts of image and deep understanding of yuv/rgb
Learn computer knowledge from scratch
PHP wechat red packet grabbing algorithm
Phpcms realizes the direct Alipay payment function of orders
[QT] QT multithreading development - four methods to realize multithreading design
Jerry's modification does not require long press the boot function [chapter]
Il n'est pas nécessaire d'appuyer longtemps sur la fonction de démarrage pour modifier Jelly [chapitre]