Skip to content

Commit f1e26e3

Browse files
committed
2025-10-23 补全第 300 ~ 399 题的题目解析(增加 20 道题)
1 parent 9aa046c commit f1e26e3

24 files changed

+2461
-4
lines changed

docs/00_preface/00_05_solutions_list.md

Lines changed: 21 additions & 1 deletion
Large diffs are not rendered by default.

docs/others/update_time.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
## 2025-10
22

3+
- 2025-10-23 补全第 300 ~ 399 题的题目解析(增加 20 道题)
34
- 2025-10-22 补充第 300 ~ 399 题的题目解析(增加 13 道题)
45
- 2025-10-21 补充第 300 ~ 399 题的题目解析(增加 13 道题)
56
- 2025-10-20 补全第 200 ~ 299 题的题目解析(增加 33 道题)

docs/solutions/0001-0099/remove-element.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,13 @@
99

1010
## 题目大意
1111

12-
**描述**给定一个数组 $nums$,和一个值 $val$。
12+
**描述**
1313

14-
**要求**:不使用额外数组空间,将数组中所有数值等于 $val$ 值的元素移除掉,并且返回新数组的长度。
14+
给定一个数组 $nums$,和一个值 $val$。
15+
16+
**要求**
17+
18+
不使用额外数组空间,将数组中所有数值等于 $val$ 值的元素移除掉,并且返回新数组的长度。
1519

1620
**说明**
1721

@@ -43,7 +47,7 @@
4347

4448
1. 使用两个指针 $slow$,$fast$。$slow$ 指向处理好的非 $val$ 值元素数组的尾部,$fast$ 指针指向当前待处理元素。
4549
2. 不断向右移动 $fast$ 指针,每次移动到非 $val$ 值的元素,则将左右指针对应的数交换,交换同时将 $slow$ 右移。
46-
3. 这样就将非 $val$ 值的元素进行前移,$slow$ 指针左边均为处理好的非 $val$ 值元素,而从 $slow$ 指针指向的位置开始, $fast$ 指针左边都为 $val $值。
50+
3. 这样就将非 $val$ 值的元素进行前移,$slow$ 指针左边均为处理好的非 $val$ 值元素,而从 $slow$ 指针指向的位置开始,$fast$ 指针左边都为 $val$ 值。
4751
4. 遍历结束之后,则所有 $val$ 值元素都移动到了右侧,且保持了非零数的相对位置。此时 $slow$ 就是新数组的长度。
4852

4953
### 思路 1:代码
@@ -56,6 +60,8 @@ class Solution:
5660
while fast < len(nums):
5761
if nums[fast] != val:
5862
nums[slow], nums[fast] = nums[fast], nums[slow]
63+
# 此处也可以直接赋值,因为最终 nums[fast] 会被移除掉
64+
# nums[slow] = nums[fast]
5965
slow += 1
6066
fast += 1
6167
return slow

docs/solutions/0300-0399/index.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,23 @@
11
## 本章内容
22

33
- [0300. 最长递增子序列](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/longest-increasing-subsequence.md)
4+
- [0301. 删除无效的括号](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/remove-invalid-parentheses.md)
5+
- [0302. 包含全部黑色像素的最小矩形](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/smallest-rectangle-enclosing-black-pixels.md)
46
- [0303. 区域和检索 - 数组不可变](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/range-sum-query-immutable.md)
57
- [0304. 二维区域和检索 - 矩阵不可变](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/range-sum-query-2d-immutable.md)
8+
- [0305. 岛屿数量 II](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/number-of-islands-ii.md)
69
- [0306. 累加数](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/additive-number.md)
710
- [0307. 区域和检索 - 数组可修改](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/range-sum-query-mutable.md)
11+
- [0308. 二维区域和检索 - 矩阵可修改](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/range-sum-query-2d-mutable.md)
812
- [0309. 买卖股票的最佳时机含冷冻期](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/best-time-to-buy-and-sell-stock-with-cooldown.md)
913
- [0310. 最小高度树](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/minimum-height-trees.md)
14+
- [0311. 稀疏矩阵的乘法](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/sparse-matrix-multiplication.md)
1015
- [0312. 戳气球](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/burst-balloons.md)
16+
- [0313. 超级丑数](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/super-ugly-number.md)
1117
- [0314. 二叉树的垂直遍历](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/binary-tree-vertical-order-traversal.md)
1218
- [0315. 计算右侧小于当前元素的个数](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/count-of-smaller-numbers-after-self.md)
1319
- [0316. 去除重复字母](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/remove-duplicate-letters.md)
20+
- [0317. 离建筑物最近的距离](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/shortest-distance-from-all-buildings.md)
1421
- [0318. 最大单词长度乘积](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/maximum-product-of-word-lengths.md)
1522
- [0319. 灯泡开关](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/bulb-switcher.md)
1623
- [0320. 列举单词的全部缩写](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/generalized-abbreviation.md)
@@ -23,11 +30,16 @@
2330
- [0327. 区间和的个数](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/count-of-range-sum.md)
2431
- [0328. 奇偶链表](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/odd-even-linked-list.md)
2532
- [0329. 矩阵中的最长递增路径](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/longest-increasing-path-in-a-matrix.md)
33+
- [0330. 按要求补齐数组](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/patching-array.md)
34+
- [0331. 验证二叉树的前序序列化](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/verify-preorder-serialization-of-a-binary-tree.md)
35+
- [0332. 重新安排行程](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/reconstruct-itinerary.md)
2636
- [0333. 最大二叉搜索子树](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/largest-bst-subtree.md)
2737
- [0334. 递增的三元子序列](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/increasing-triplet-subsequence.md)
38+
- [0335. 路径交叉](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/self-crossing.md)
2839
- [0336. 回文对](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/palindrome-pairs.md)
2940
- [0337. 打家劫舍 III](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/house-robber-iii.md)
3041
- [0338. 比特位计数](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/counting-bits.md)
42+
- [0339. 嵌套列表加权和](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/nested-list-weight-sum.md)
3143
- [0340. 至多包含 K 个不同字符的最长子串](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/longest-substring-with-at-most-k-distinct-characters.md)
3244
- [0341. 扁平化嵌套列表迭代器](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/flatten-nested-list-iterator.md)
3345
- [0342. 4的幂](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/power-of-four.md)
@@ -46,16 +58,21 @@
4658
- [0355. 设计推特](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/design-twitter.md)
4759
- [0356. 直线镜像](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/line-reflection.md)
4860
- [0357. 统计各位数字都不同的数字个数](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/count-numbers-with-unique-digits.md)
61+
- [0358. K 距离间隔重排字符串](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/rearrange-string-k-distance-apart.md)
4962
- [0359. 日志速率限制器](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/logger-rate-limiter.md)
5063
- [0360. 有序转化数组](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/sort-transformed-array.md)
5164
- [0361. 轰炸敌人](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/bomb-enemy.md)
5265
- [0362. 敲击计数器](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/design-hit-counter.md)
5366
- [0363. 矩形区域不超过 K 的最大数值和](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/max-sum-of-rectangle-no-larger-than-k.md)
67+
- [0364. 嵌套列表加权和 II](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/nested-list-weight-sum-ii.md)
68+
- [0365. 水壶问题](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/water-and-jug-problem.md)
5469
- [0366. 寻找二叉树的叶子节点](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/find-leaves-of-binary-tree.md)
5570
- [0367. 有效的完全平方数](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/valid-perfect-square.md)
5671
- [0368. 最大整除子集](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/largest-divisible-subset.md)
72+
- [0369. 给单链表加一](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/plus-one-linked-list.md)
5773
- [0370. 区间加法](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/range-addition.md)
5874
- [0371. 两整数之和](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/sum-of-two-integers.md)
75+
- [0372. 超级次方](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/super-pow.md)
5976
- [0373. 查找和最小的 K 对数字](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/find-k-pairs-with-smallest-sums.md)
6077
- [0374. 猜数字大小](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/guess-number-higher-or-lower.md)
6178
- [0375. 猜数字大小 II](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/guess-number-higher-or-lower-ii.md)
@@ -76,7 +93,10 @@
7693
- [0390. 消除游戏](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/elimination-game.md)
7794
- [0391. 完美矩形](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/perfect-rectangle.md)
7895
- [0392. 判断子序列](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/is-subsequence.md)
96+
- [0393. UTF-8 编码验证](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/utf-8-validation.md)
7997
- [0394. 字符串解码](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/decode-string.md)
8098
- [0395. 至少有 K 个重复字符的最长子串](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/longest-substring-with-at-least-k-repeating-characters.md)
99+
- [0396. 旋转函数](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/rotate-function.md)
81100
- [0397. 整数替换](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/integer-replacement.md)
101+
- [0398. 随机数索引](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/random-pick-index.md)
82102
- [0399. 除法求值](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/evaluate-division.md)
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
# [0364. 嵌套列表加权和 II](https://leetcode.cn/problems/nested-list-weight-sum-ii/)
2+
3+
- 标签:栈、深度优先搜索、广度优先搜索
4+
- 难度:中等
5+
6+
## 题目链接
7+
8+
- [0364. 嵌套列表加权和 II - 力扣](https://leetcode.cn/problems/nested-list-weight-sum-ii/)
9+
10+
## 题目大意
11+
12+
**描述**
13+
14+
给定一个整数嵌套列表 $nestedList$,每一个元素要么是一个整数,要么是一个列表(这个列表中的每个元素也同样是整数或列表)。
15+
16+
- 整数的「深度」取决于它位于多少个列表内部。例如,嵌套列表 $[1,[2,2],[[3],2],1]$ 的每个整数的值都等于它的深度。令 $maxDepth$ 是任意整数的「最大深度」。
17+
- 整数的「权重」为 $maxDepth$ - (整数的深度) $+ 1$。
18+
19+
**要求**
20+
21+
将 $nestedList$ 列表中每个整数先乘权重再求和,返回该加权和。
22+
23+
**说明**
24+
25+
- $1 \le nestedList.length \le 50$。
26+
- 嵌套列表中整数的值在范围 $[-10^{3}, 10^{3}]$。
27+
- 任意整数的最大「深度」小于等于 $50$。
28+
- 没有空列表。
29+
30+
**示例**
31+
32+
- 示例 1:
33+
34+
![](https://assets.leetcode.com/uploads/2021/03/27/nestedlistweightsumiiex1.png)
35+
36+
```python
37+
输入:nestedList = [[1,1],2,[1,1]]
38+
输出:8
39+
解释:41 在深度为 1 的位置, 一个 2 在深度为 2 的位置。
40+
1*1 + 1*1 + 2*2 + 1*1 + 1*1 = 8
41+
```
42+
43+
- 示例 2:
44+
45+
![](https://assets.leetcode.com/uploads/2021/03/27/nestedlistweightsumiiex2.png)
46+
47+
```python
48+
输入:nestedList = [1,[4,[6]]]
49+
输出:17
50+
解释:一个 1 在深度为 3 的位置, 一个 4 在深度为 2 的位置,一个 6 在深度为 1 的位置。
51+
1*3 + 4*2 + 6*1 = 17
52+
```
53+
54+
## 解题思路
55+
56+
### 思路 1:两次遍历
57+
58+
1. **第一次遍历**:计算最大深度 $maxDepth$。使用深度优先搜索遍历整个嵌套列表,记录每个整数的深度,并更新最大深度。
59+
60+
2. **第二次遍历**:计算加权和。再次遍历嵌套列表,对于每个整数,其权重为 $maxDepth - depth + 1$,其中 $depth$ 是该整数的深度。将所有整数的值乘以其权重后求和。
61+
62+
具体步骤:
63+
64+
- 定义递归函数 `getMaxDepth(nestedList, depth)` 用于计算最大深度。
65+
- 定义递归函数 `calculateSum(nestedList, depth, maxDepth)` 用于计算加权和。
66+
- 对每个 `NestedInteger` 元素:
67+
- 如果是整数,则在计算最大深度时进行比较和更新;在计算加权和时累加当前值乘以权重。
68+
- 如果是列表,则递归地处理其内部元素。
69+
70+
71+
### 思路 1:代码
72+
73+
```python
74+
# """
75+
# This is the interface that allows for creating nested lists.
76+
# You should not implement it, or speculate about its implementation
77+
# """
78+
#class NestedInteger:
79+
# def __init__(self, value=None):
80+
# """
81+
# If value is not specified, initializes an empty list.
82+
# Otherwise initializes a single integer equal to value.
83+
# """
84+
#
85+
# def isInteger(self):
86+
# """
87+
# @return True if this NestedInteger holds a single integer, rather than a nested list.
88+
# :rtype bool
89+
# """
90+
#
91+
# def add(self, elem):
92+
# """
93+
# Set this NestedInteger to hold a nested list and adds a nested integer elem to it.
94+
# :rtype void
95+
# """
96+
#
97+
# def setInteger(self, value):
98+
# """
99+
# Set this NestedInteger to hold a single integer equal to value.
100+
# :rtype void
101+
# """
102+
#
103+
# def getInteger(self):
104+
# """
105+
# @return the single integer that this NestedInteger holds, if it holds a single integer
106+
# Return None if this NestedInteger holds a nested list
107+
# :rtype int
108+
# """
109+
#
110+
# def getList(self):
111+
# """
112+
# @return the nested list that this NestedInteger holds, if it holds a nested list
113+
# Return None if this NestedInteger holds a single integer
114+
# :rtype List[NestedInteger]
115+
# """
116+
117+
class Solution:
118+
def depthSumInverse(self, nestedList: List[NestedInteger]) -> int:
119+
# 第一次遍历:计算最大深度
120+
max_depth = self.getMaxDepth(nestedList, 1)
121+
122+
# 第二次遍历:计算加权和
123+
return self.calculateSum(nestedList, 1, max_depth)
124+
125+
def getMaxDepth(self, nestedList: List[NestedInteger], depth: int) -> int:
126+
"""计算嵌套列表的最大深度"""
127+
max_depth = depth
128+
129+
for item in nestedList:
130+
if item.isInteger():
131+
# 如果是整数,更新最大深度
132+
max_depth = max(max_depth, depth)
133+
else:
134+
# 如果是列表,递归计算子列表的最大深度
135+
max_depth = max(max_depth, self.getMaxDepth(item.getList(), depth + 1))
136+
137+
return max_depth
138+
139+
def calculateSum(self, nestedList: List[NestedInteger], depth: int, max_depth: int) -> int:
140+
"""计算加权和"""
141+
total_sum = 0
142+
143+
for item in nestedList:
144+
if item.isInteger():
145+
# 如果是整数,计算其加权贡献
146+
# 权重 = max_depth - depth + 1
147+
weight = max_depth - depth + 1
148+
total_sum += item.getInteger() * weight
149+
else:
150+
# 如果是列表,递归计算子列表的加权和
151+
total_sum += self.calculateSum(item.getList(), depth + 1, max_depth)
152+
153+
return total_sum
154+
```
155+
156+
### 思路 1:复杂度分析
157+
158+
- **时间复杂度**:$O(n)$,其中 $n$ 是嵌套列表中所有整数的总数。需要遍历两次嵌套列表,每次遍历的时间复杂度都是 $O(n)$。
159+
- **空间复杂度**:$O(d)$,其中 $d$ 是嵌套列表的最大深度。递归调用栈的深度最多为 $d$。

0 commit comments

Comments
 (0)