当前位置:网站首页>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
边栏推荐
- ESLint 报错
- Leetcode -- the nearest common ancestor of 236 binary tree
- Vscode auto format
- Beautiful and intelligent, Haval H6 supreme+ makes Yuanxiao travel safer
- [IDL] Research
- UE5——AI追逐(蓝图、行为树)
- 两数之和,求目标值
- Project practice, redis cluster technology learning (VIII)
- 判断数组中是否存在重复元素
- Alibaba cloud Prometheus monitoring service
猜你喜欢
Notes de base sur les plans illusoires d'IA (triés en 10 000 mots)
[ue5] blueprint making simple mine tutorial
Alibaba cloud SMS service
SAP Spartacus express checkout design
The latest progress and development trend of 2022 intelligent voice technology
两数之和,求目标值
Unreal material editor foundation - how to connect a basic material
Blender多鏡頭(多機比特)切換
MySQL index
[ue5] two implementation methods of AI random roaming blueprint (role blueprint and behavior tree)
随机推荐
MySQL index
How to judge the quality of primary market projects when the market is depressed?
Career planning and development
Database -- acid of transaction -- introduction / explanation
Project practice, redis cluster technology learning (10)
ERROR 1118 (42000): Row size too large (&gt; 8126)
webUI自动化学习
UE4夜间打光笔记
测试--面试题总结
Leetcode -- the nearest common ancestor of 236 binary tree
Sil/pil test of matlab code generation
Alibaba cloud SLS log service
[leetcode] sword finger offer 53 - I. find the number I in the sorted array
【Visual Studio】每次打开一个Unity3D的脚本,都会自动重新打开一个新的VS2017
2837xd code generation module learning (2) -- ADC, epwm module, timer0
ICLR 2022: how does AI recognize "things I haven't seen"?
UE5——AI追逐(蓝图、行为树)
2837xd代码生成模块学习(2)——ADC、ePWM模块、Timer0
Spatial interpretation | comprehensive analysis of spatial structure of primary liver cancer
Project practice, redis cluster technology learning (VIII)