当前位置:网站首页>判断数组中是否存在重复元素
判断数组中是否存在重复元素
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)
个人见解:以空间换时间的方式进行
边栏推荐
- 【UE5】蓝图制作简单地雷教程
- 【Unity3D】制作进度条——让Image同时具有Filled和Sliced的功能
- Basic notes of illusory AI blueprint (10000 words)
- Following nym, the new project Galaxy token announced by coinlist is gal
- 2837xd代码生成模块学习(1)——GPIO模块
- [ue5] two implementation methods of AI random roaming blueprint (role blueprint and behavior tree)
- 【JetBrain Rider】构建项目出现异常:未找到导入的项目“D:\VisualStudio2017\IDE\MSBuild\15.0\Bin\Roslyn\Microsoft.CSh
- [IDL] Research
- Translation d30 (with AC code POJ 28:sum number)
- 阿里云Prometheus监控服务
猜你喜欢

【UE5】AI随机漫游蓝图两种实现方法(角色蓝图、行为树)

Blender多镜头(多机位)切换

【UE5】蓝图制作简单地雷教程

webUI自动化学习

【Unity3D】嵌套使用Layout Group制作拥有动态子物体高度的Scroll View
![[illusory] automatic door blueprint notes](/img/7a/14a66e5b623c7740dc91a15a328b10.png)
[illusory] automatic door blueprint notes

2837xd Code Generation - Supplement (1)

Ctrip starts mixed office. How can small and medium-sized enterprises achieve mixed office?

Feature (5): how to organize information

A model can do two things: image annotation and image reading Q & A. VQA accuracy is close to human level | demo can be played
随机推荐
Beautiful and intelligent, Haval H6 supreme+ makes Yuanxiao travel safer
Zlib download and use
Project practice, redis cluster technology learning (11)
【教程】如何让VisualStudio的HelpViewer帮助文档独立运行
Judging right triangle in C language
C language: making barrels
Attack and defense world web advanced area unserialize3
Remember the use of add method once
MySQL index
渗透测试的介绍和防范
Metaclass type and using metaclass to implement model class ORM
Deep understanding of redis cache avalanche / cache breakdown / cache penetration
The primary market project galaxy will conduct public offering on coinlist on February 17
Large neural networks may be beginning to realize: the chief scientist of openai leads to controversy, and everyone quarrels
Skywalking理论与实践
Data insertion in C language
虚幻——动画蓝图、状态机制作人物走跑跳动作
2837xd Code Generation - Supplement (1)
虛幻AI藍圖基礎筆記(萬字整理)
Project practice, redis cluster technology learning (VII)