当前位置:网站首页>判断数组中是否存在重复元素
判断数组中是否存在重复元素
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)
个人见解:以空间换时间的方式进行
边栏推荐
- Brief analysis of edgedb architecture
- Database -- acid of transaction -- introduction / explanation
- ue虚幻引擎程序化植物生成器设置——如何快速生成大片森林
- Blender摄像机环绕运动、动画渲染、视频合成
- 2837xd代码生成模块学习(2)——ADC、ePWM模块、Timer0
- UE illusory engine programmed plant generator setup -- how to quickly generate large forests
- 阿里云SLS日志服务
- ue4材质的入门和原理笔记
- 【leetcode】33. Search rotation sort array
- Configuration programmée du générateur de plantes du moteur illusoire UE - - Comment générer rapidement une grande forêt
猜你喜欢
Aiphacode is not a substitute for programmers, but a tool for developers
Blender stone carving
[ue5] blueprint making simple mine tutorial
【虚幻】过场动画笔记
2837xd code generation - stateflow (2)
Attack and defense world web advanced area unserialize3
The latest progress and development trend of 2022 intelligent voice technology
This monitoring system makes workers tremble: turnover intention and fishing can be monitored. After the dispute, the product page has 404
【虚幻】武器插槽:拾取武器
ESLint 报错
随机推荐
阿里云SLS日志服务
UE5——AI追逐(藍圖、行為樹)
Blender海洋制作
ERROR 1118 (42000): Row size too large (&gt; 8126)
【UE5】动画重定向:如何将幻塔人物导入进游戏玩耍
Zlib download and use
Career planning and development
2837xd code generation - stateflow (3)
Junit5 支持suite的方法
MySQL transaction
Alibaba cloud ack introduction
Blender摄像机环绕运动、动画渲染、视频合成
Mobile mall app solution: how much is it to make an app? Detailed explanation of APP mall development content
Basic notes of illusory AI blueprint (10000 words)
Skywalking理论与实践
ESLint 报错
【leetcode】33. Search rotation sort array
2837xd code generation - Supplement (3)
Introduction et prévention des essais de pénétration
[unreal] animation notes of the scene