当前位置:网站首页>Determine whether there are duplicate elements in the array
Determine whether there are duplicate elements in the array
2022-07-02 10:21:00 【Lost ~ know to return】
Array
Array
principle
python How to use
Arrangement of elements in the list
sort Method
Given an array of integers , Determine whether there are duplicate elements .
If a value exists, it appears at least twice in the array , The function returns true . If every element in the array is different , Then return to false .
Their thinking
- Sort existing arrays ,
- Compare the elements in the array bit by bit
# The first version of the code :
class Solution(object):
def containsDuplicate(self, nums):
""" :type nums: List[int] :rtype: bool """
#1. Sort the elements in the list
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
The error message is as above : Loop variable setting error , The value of the loop variable is always 0, The current two elements are the same , Can be returned as true, When there is only one element in the list , Unable to output ,
The modified code is as follows :
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
The loop variable is set incorrectly ,i The value of is always 0, At this time, the element cannot be shifted for comparison , Only compare the first and second , The last two elements will not be compared
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
Current The time complexity is :O(nlogn), The space complexity is 1
Solution 2 :
Hashtable
class Solution(object):
def containsDuplicate(self, nums):
""" :type nums: List[int] :rtype: bool """
# Method 2 : Hashtable
visted = set()
for num in nums:
if num in visted:
return True
visted.add(num)
return False
Time complexity :O(N), Spatial complexity O(N)
Personal opinion : Space for time
边栏推荐
- Junit5 支持suite的方法
- 高考那些事
- 【虚幻4】UMG组件的简介与使用(更新中...)
- Blender多镜头(多机位)切换
- [IDL] Research
- Vscode set JSON file to format automatically after saving
- Centos7 one click compilation and installation of PHP script
- Blender model import UE, collision settings
- Summary of demand R & D process nodes and key outputs
- 2837xd code generation module learning (1) -- GPIO module
猜你喜欢
Blender model import UE, collision settings
Blender多镜头(多机位)切换
Large neural networks may be beginning to realize: the chief scientist of openai leads to controversy, and everyone quarrels
Alibaba cloud SMS service
2837xd代码生成模块学习(3)——IIC、eCAN、SCI、Watchdog、eCAP模块
[ue5] animation redirection: how to import magic tower characters into the game
webUI自动化学习
Skywalking理论与实践
2.14 is it Valentine's day or Valentine's day when the mainstream market continues to fluctuate and wait for changes?
[illusory] automatic door blueprint notes
随机推荐
Following nym, the new project Galaxy token announced by coinlist is gal
【MySQL】连接MySQL时出现异常:Connection must be valid and open
How to handle error logic gracefully
What wires are suitable for wiring on bread board?
合并有序数列
Ue5 - AI pursuit (blueprint, behavior tree)
Project practice, redis cluster technology learning (12)
【JetBrain Rider】构建项目出现异常:未找到导入的项目“D:\VisualStudio2017\IDE\MSBuild\15.0\Bin\Roslyn\Microsoft.CSh
Blender海洋制作
go语言入门
ERROR 1118 (42000): Row size too large (&gt; 8126)
2837xd code generation module learning (3) -- IIC, ECAN, SCI, watchdog, ECAP modules
XA Transaction SQL Statements
webUI自动化学习
Translation d30 (with AC code POJ 28:sum number)
pytest--之测试报告allure配置
【Lua】常见知识点汇总(包含常见面试考点)
2837xd code generation module learning (1) -- GPIO module
2021-10-04
渗透测试的介绍和防范