当前位置:网站首页>判断数组中是否存在重复元素
判断数组中是否存在重复元素
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)
个人见解:以空间换时间的方式进行
边栏推荐
- Project practice, redis cluster technology learning (IX)
- pytest--之测试报告allure配置
- Blender ocean production
- How does {} prevent SQL injection? What is its underlying principle?
- How to handle error logic gracefully
- ue虚幻引擎程序化植物生成器设置——如何快速生成大片森林
- Deep understanding of redis cache avalanche / cache breakdown / cache penetration
- 2837xd code generation module learning (2) -- ADC, epwm module, timer0
- C language: making barrels
- Minimum number of C language
猜你喜欢

UE4 night lighting notes

Leetcode -- the nearest common ancestor of 236 binary tree

The primary market project galaxy will conduct public offering on coinlist on February 17

2837xd代码生成模块学习(1)——GPIO模块

Blender摄像机环绕运动、动画渲染、视频合成

Blender volume fog

This article takes you to learn in detail what is fiber to home FTTH

Attack and defense world web advanced area unserialize3

Mixed development of uni app -- Taking wechat applet as an example

UE5——AI追逐(蓝图、行为树)
随机推荐
Skywalking理论与实践
[illusory] weapon slot: pick up weapons
Project practice, redis cluster technology learning (VII)
Ue5 - ai Pursuit (Blueprint, Behavior tree)
pytest--之测试报告allure配置
Following nym, the new project Galaxy token announced by coinlist is gal
滲透測試的介紹和防範
2837xd code generation module learning (3) -- IIC, ECAN, SCI, watchdog, ECAP modules
【UE5】动画重定向:如何将幻塔人物导入进游戏玩耍
Centos7 one click compilation and installation of PHP script
Commutateur Multi - lentilles Blender
Translation d30 (with AC code POJ 28:sum number)
Brief analysis of edgedb architecture
【避坑指南】Unity3D项目接入腾讯Bugly工具时遇到的坑
2837xd code generation - Supplement (2)
高考那些事
Alibaba cloud SLS log service
Network real-time video streaming based on OpenCV
How to achieve the top progress bar effect in background management projects
[ue5] two implementation methods of AI random roaming blueprint (role blueprint and behavior tree)