抱歉,您的浏览器无法访问本站
本页面需要浏览器支持(启用)JavaScript
了解详情 >

前言

这些题目来自于leetcode上发布的一篇讨论有没有人一起从零开始刷力扣与官方推出的力扣刷题攻略,这两篇题单大同小异,都是我认为不错的leetcode入门题单。

本文旨在记录自己的刷题历程,希望早日将这些刷透,烂熟于心。


645. Set Mismatch 错误的集合

原题链接:

https://leetcode.cn/problems/set-mismatch/description/

题目描述:

正常状态下的数组是1-N个元素,但是有个数字重复出现了,导致覆盖了一个原有的数字,请给出重复的数字与缺失的数字

方法一:哈希表

可以定义一个哈希表,统计数组中数字出现的次数,出现次数大于1即为重复的数字,未出现的元素则不会在哈希表中记录。

代码实现:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Solution:
def findErrorNums(self, nums: List[int]) -> List[int]:
count = {}
duplicate = -1
missing = -1

# 记录每个数字的出现次数
for num in nums:
if num not in count:
count[num] = 1
else:
count[num] += 1

# 找到重复的数字和丢失的数字
for i in range(1, len(nums) + 1):
if i not in count:
missing = i
elif count[i] > 1:
duplicate = i

return [duplicate, missing]
  • 时间复杂度:$$O(n)$$(仅遍历一次数组)
  • 空间复杂度:$$O(n)$$(哈希表)

697. Degree of an Array 数组的度

原题链接:

https://leetcode.cn/problems/degree-of-an-array/

题目描述:

数组的度是出现次数最多的数字的出现次数。求一个最短子数组的长度,其度等于数组的度。

方法一:遍历数组

  1. 遍历一遍数组,计算数组的度
  2. 再遍历一遍数组,找到数组中出现频数为度的数组的第一次出现的位置与最后一次出现的位置
  3. 计算每个数字的子数组长度

代码实现:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class Solution:
def findShortestSubArray(self, nums: List[int]) -> int:
# 计算数组的度
counter = {}
for num in nums:
if num in counter:
counter[num] += 1
else:
counter[num] = 1
max_degree = max(counter.values())

# 找到数组中出现频数为度的数字的第一次出现位置和最后一次出现位置
first_occurrence = {}
last_occurrence = {}
for i, num in enumerate(nums):
if num not in first_occurrence:
first_occurrence[num] = i
last_occurrence[num] = i

# 计算每个数字的子数组长度
min_length = len(nums)
for num, count in counter.items():
if count == max_degree:
min_length = min(min_length, last_occurrence[num] - first_occurrence[num] + 1)

return min_length

448. Find All Numbers Disappeared in an Array 找到所有数组中消失的数字

原题链接:

https://leetcode.cn/problems/find-all-numbers-disappeared-in-an-array/

题目大意:

给你一个含 n 个整数的数组 nums ,其中 nums[i] 在区间 [1, n] 内。请你找出所有在 [1, n] 范围内但没有出现在 nums 中的数字,并以数组的形式返回结果。

方法一:哈希表

代码实现:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Solution:
def findDisappearedNumbers(self, nums: List[int]) -> List[int]:
n = len(nums)
# 使用HashSet记录数组中出现的数字
num_set = set(nums)
# 存放缺失的数字
result = []

# 遍历 [1, n] 范围内的数字
for i in range(1, n+1):
# 如果当前数字不在HashSet中,说明缺失
if i not in num_set:
result.append(i)

return result

方法二:索引映射

代码实现:

1
2
3
4
5
6
7
8
9
10
11
12
13
class Solution:
def findDisappearedNumbers(self, nums: List[int]) -> List[int]:
n = len(nums)

# 第一次遍历,将出现的数字对应的索引位置标记为负数
for num in nums:
index = abs(num) - 1
nums[index] = -abs(nums[index])

# 第二次遍历,找出非负数的索引位置,即为缺失的数字
result = [i + 1 for i in range(n) if nums[i] > 0]

return result

442. Find All Duplicates in an Array 数组中重复的数据

原题链接:

https://leetcode.cn/problems/find-all-duplicates-in-an-array/

题目大意:

返回数组中重复出现两次的整数

方法一:原地修改数组

代码实现:

1
2
3
4
5
6
7
8
class Solution:
def findDuplicates(self, nums: List[int]) -> List[int]:
ans = []
for num in nums:
if nums[abs(num) - 1] < 0:
ans.append(abs(num))
nums[abs(num) - 1] *= - 1
return ans

评论