217. Contains Duplicate [easy] (Python)

题目链接https://leetcode.com/problems/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 i
摘要由CSDN通过智能技术生成

题目链接

https://leetcode.com/problems/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.

题目翻译

给定一个整数数组,判断其中是否包含重复元素。你要写的函数,当包含重复元素时返回 true,否则返回 false。

思路方法

首先我们可能会想到,遍历数组,对于每个元素,再检查后面所有元素是否有相同的就可以了。然而这样时间复杂度太高,会超时,所以不可取。

思路一

既然暴力查重不可取,那么我没先将数组进行排序(排序的复杂度),排序后比较每个元素与后一个元素是否相等即可。

代码

class Solution
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值