当前位置:网站首页>判断数组中是否存在重复元素
判断数组中是否存在重复元素
2022-07-02 06:36:00 【迷途~知返】
数组
原理
python的使用方法
列表中元素的排列
sort方法
给定一个整数数组,判断是否存在重复元素。
如果存在一值在数组中出现至少两次,函数返回 true 。如果数组中每个元素都不相同,则返回 false 。
解题思路
- 对现有数组进行排序,
- 对数组中的元素进行按位比较
#第一版代码:
class Solution(object):
def containsDuplicate(self, nums):
""" :type nums: List[int] :rtype: bool """
#1. 对列表中的元素进行排序
nums.sort()
print "nums", nums
n = len(nums)
print n
i = 0
# while i <= n:
# if nums[i] == nums[i+1]:
# return True
# else:
# return False
for i in range(n-1):
if nums[i] == nums[i+1]:
return True
else:
return False
i = i + 1
报错信息如上:循环变量设置错误,循环变量的值始终为0,当前两位元素相同时,可返回为true,当列表中只有一个元素中,无法进行输出,
修改后的代码如下:
class Solution(object):
def containsDuplicate(self, nums):
""" :type nums: List[int] :rtype: bool """
nums.sort()
n = len(nums)
if n <=1:
return False
else:
for i in range(n-1):
# print "i", i
if nums[i] == nums[i+1]:
return True
return False
循环变量设置有误,i的值始终为0,此时元素无法移位进行比较,只会比较第一和第二位,不会比较到后两位元素
class Solution(object):
def containsDuplicate(self, nums):
""" :type nums: List[int] :rtype: bool """
nums.sort()
n = len(nums)
for i in range(n-1):
if nums[i] == nums[i+1]:
return True
i = i + 1
return False
当前的 时间复杂度为:O(nlogn),空间复杂度为1
解法二:
哈希表
class Solution(object):
def containsDuplicate(self, nums):
""" :type nums: List[int] :rtype: bool """
#方法二:哈希表
visted = set()
for num in nums:
if num in visted:
return True
visted.add(num)
return False
时间复杂度:O(N),空间复杂度O(N)
个人见解:以空间换时间的方式进行
边栏推荐
- 阿里云ack介绍
- Judging right triangle in C language
- The latest progress and development trend of 2022 intelligent voice technology
- Skywalking theory and Practice
- 2837xd code generation - Supplement (3)
- Project practice, redis cluster technology learning (IX)
- Centos7 one click compilation and installation of PHP script
- ESLint 报错
- Project practice, redis cluster technology learning (VII)
- It is the most difficult to teach AI to play iron fist frame by frame. Now arcade game lovers have something
猜你喜欢

【Unity3D】嵌套使用Layout Group制作拥有动态子物体高度的Scroll View

Junit5 supports suite methods

2.14 is it Valentine's day or Valentine's day when the mainstream market continues to fluctuate and wait for changes?

Remember the use of add method once

A model can do two things: image annotation and image reading Q & A. VQA accuracy is close to human level | demo can be played

Mock Server基本使用方法

阿里云Prometheus监控服务

2837xd代码生成模块学习(2)——ADC、ePWM模块、Timer0

Basic notes of illusory AI blueprint (10000 words)

Edge computing accelerates live video scenes: clearer, smoother, and more real-time
随机推荐
【虚幻】按键开门蓝图笔记
go语言入门
Basic notes of illusory AI blueprint (10000 words)
Project practice, redis cluster technology learning (IX)
Tee command usage example
Mobile mall app solution: how much is it to make an app? Detailed explanation of APP mall development content
Judging right triangle in C language
Alibaba cloud ack introduction
MySQL transaction
How does {} prevent SQL injection? What is its underlying principle?
【教程】如何让VisualStudio的HelpViewer帮助文档独立运行
虚幻AI蓝图基础笔记(万字整理)
职业规划和发展
【避坑指南】使用UGUI遇到的坑:Text组件无法首行缩进两格
[unreal] animation notes of the scene
【避坑指南】Unity3D项目接入腾讯Bugly工具时遇到的坑
Junit4运行mvn test 测试套件升级方案
Blender多镜头(多机位)切换
MySQL index
【Unity3D】嵌套使用Layout Group制作拥有动态子物体高度的Scroll View