当前位置:网站首页>判断数组中是否存在重复元素
判断数组中是否存在重复元素
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)
个人见解:以空间换时间的方式进行
边栏推荐
- Mock Server基本使用方法
- webUI自动化学习
- Feature (5): how to organize information
- A model can do two things: image annotation and image reading Q & A. VQA accuracy is close to human level | demo can be played
- [unreal] key to open the door blueprint notes
- 【避坑指南】Unity3D项目接入腾讯Bugly工具时遇到的坑
- Blender multi lens (multi stand) switching
- C language strawberry
- Blender体积雾
- MySQL index
猜你喜欢

2837xd code generation module learning (4) -- idle_ task、Simulink Coder

Commutateur Multi - lentilles Blender

【虚幻】自动门蓝图笔记

This monitoring system makes workers tremble: turnover intention and fishing can be monitored. After the dispute, the product page has 404

Eslint reports an error

Illusion -- Animation blueprint, state machine production, character walking, running and jumping action

【虚幻4】UMG组件的简介与使用(更新中...)

Applet development summary

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

滲透測試的介紹和防範
随机推荐
This monitoring system makes workers tremble: turnover intention and fishing can be monitored. After the dispute, the product page has 404
Introduction et prévention des essais de pénétration
Mixed development of uni app -- Taking wechat applet as an example
【MySQL】连接MySQL时出现异常:Connection must be valid and open
AutoCAD - layer Linetype
【UE5】动画重定向:如何将幻塔人物导入进游戏玩耍
Summary of demand R & D process nodes and key outputs
2837xd code generation - stateflow (1)
Mock Server基本使用方法
Alibaba cloud SLS log service
【UE5】AI随机漫游蓝图两种实现方法(角色蓝图、行为树)
Remember a simple Oracle offline data migration to tidb process
虛幻AI藍圖基礎筆記(萬字整理)
High level application of SQL statements in MySQL database (II)
Blender多镜头(多机位)切换
高考那些事
Database -- acid of transaction -- introduction / explanation
Blender stone carving
Blender camera surround motion, animation rendering, video synthesis
Project practice, redis cluster technology learning (11)