当前位置:网站首页>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
边栏推荐
- 滲透測試的介紹和防範
- Junit4 runs MVN test test suite upgrade scheme
- pytest框架实现前后置
- Unreal material editor foundation - how to connect a basic material
- Alibaba cloud SMS service
- Project practice, redis cluster technology learning (IX)
- 阿里云短信服务
- 【虚幻】过场动画笔记
- Pycaret | a few lines of code to solve machine learning modeling
- Blender stone carving
猜你喜欢

2.14 is it Valentine's day or Valentine's day when the mainstream market continues to fluctuate and wait for changes?

allure--常用配置项

Ue5 - ai Pursuit (Blueprint, Behavior tree)

两数之和,求目标值

UE4夜间打光笔记
What is the relationship between realizing page watermarking and mutationobserver?
![[illusory] weapon slot: pick up weapons](/img/a7/1e395fc9cdfd0359e7ae4d2313290d.png)
[illusory] weapon slot: pick up weapons

MySQL index

【虚幻】自动门蓝图笔记

A model can do two things: image annotation and image reading Q & A. VQA accuracy is close to human level | demo can be played
随机推荐
Blender multi lens (multi stand) switching
虚幻——动画蓝图、状态机制作人物走跑跳动作
Remember the use of add method once
【Unity3D】嵌套使用Layout Group制作拥有动态子物体高度的Scroll View
Large neural networks may be beginning to realize: the chief scientist of openai leads to controversy, and everyone quarrels
Notes de base sur les plans illusoires d'IA (triés en 10 000 mots)
【避坑指南】使用UGUI遇到的坑:Text组件无法首行缩进两格
Junit4运行mvn test 测试套件升级方案
【虚幻4】从U3D到UE4的转型之路
Summary of demand R & D process nodes and key outputs
职业规划和发展
[illusory] weapon slot: pick up weapons
Configuration programmée du générateur de plantes du moteur illusoire UE - - Comment générer rapidement une grande forêt
2837xd代码生成模块学习(3)——IIC、eCAN、SCI、Watchdog、eCAP模块
[leetcode] sword finger offer 53 - I. find the number I in the sorted array
【避坑指南】Unity3D项目接入腾讯Bugly工具时遇到的坑
【Unity3D】无法正确获取RectTransform的属性值导致计算出错
ERROR 1118 (42000): Row size too large (&gt; 8126)
2837xd code generation module learning (1) -- GPIO module
Matlab代码生成之SIL/PIL测试