当前位置:网站首页>判断数组中是否存在重复元素
判断数组中是否存在重复元素
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)
个人见解:以空间换时间的方式进行
边栏推荐
- 阿里云Prometheus监控服务
- How to judge the quality of primary market projects when the market is depressed?
- 高考那些事
- Blender multi lens (multi stand) switching
- How to achieve the top progress bar effect in background management projects
- Mobile mall app solution: how much is it to make an app? Detailed explanation of APP mall development content
- [200 Shengxin literatures] 95 multiomics exploration TNBC
- MySQL transaction
- Ctrip starts mixed office. How can small and medium-sized enterprises achieve mixed office?
- 【Lua】常见知识点汇总(包含常见面试考点)
猜你喜欢
Aiphacode is not a substitute for programmers, but a tool for developers
What is the relationship between realizing page watermarking and mutationobserver?
MySQL index
allure--常用配置项
Mixed development of uni app -- Taking wechat applet as an example
[illusory] automatic door blueprint notes
Leetcode -- the nearest common ancestor of 236 binary tree
Alibaba cloud Prometheus monitoring service
The primary market project galaxy will conduct public offering on coinlist on February 17
Junit5 支持suite的方法
随机推荐
Project practice, redis cluster technology learning (IX)
Beautiful and intelligent, Haval H6 supreme+ makes Yuanxiao travel safer
2837xd代码生成模块学习(3)——IIC、eCAN、SCI、Watchdog、eCAP模块
[illusory] automatic door blueprint notes
虛幻AI藍圖基礎筆記(萬字整理)
Matlab代码生成之SIL/PIL测试
Mobile mall app solution: how much is it to make an app? Detailed explanation of APP mall development content
【UE5】AI随机漫游蓝图两种实现方法(角色蓝图、行为树)
高考那些事
【Unity3D】嵌套使用Layout Group制作拥有动态子物体高度的Scroll View
Message mechanism -- getting to know messages and message queues for the first time
[ue5] blueprint making simple mine tutorial
2837xd code generation module learning (2) -- ADC, epwm module, timer0
ICLR 2022: how does AI recognize "things I haven't seen"?
【Unity3D】无法正确获取RectTransform的属性值导致计算出错
Remember the use of add method once
It is the most difficult to teach AI to play iron fist frame by frame. Now arcade game lovers have something
阿里云短信服务
[Yu Yue education] University Physics (Electromagnetics) reference materials of Taizhou College of science and technology, Nanjing University of Technology
AutoCAD - layer Linetype