Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion docs/00_preface/00_05_solutions_list.md

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions docs/others/update_time.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## 2025-10

- 2025-10-23 补全第 300 ~ 399 题的题目解析(增加 20 道题)
- 2025-10-22 补充第 300 ~ 399 题的题目解析(增加 13 道题)
- 2025-10-21 补充第 300 ~ 399 题的题目解析(增加 13 道题)
- 2025-10-20 补全第 200 ~ 299 题的题目解析(增加 33 道题)
Expand Down
12 changes: 9 additions & 3 deletions docs/solutions/0001-0099/remove-element.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,13 @@

## 题目大意

**描述**:给定一个数组 $nums$,和一个值 $val$。
**描述**:

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

**要求**:

不使用额外数组空间,将数组中所有数值等于 $val$ 值的元素移除掉,并且返回新数组的长度。

**说明**:

Expand Down Expand Up @@ -43,7 +47,7 @@

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

### 思路 1:代码
Expand All @@ -56,6 +60,8 @@ class Solution:
while fast < len(nums):
if nums[fast] != val:
nums[slow], nums[fast] = nums[fast], nums[slow]
# 此处也可以直接赋值,因为最终 nums[fast] 会被移除掉
# nums[slow] = nums[fast]
slow += 1
fast += 1
return slow
Expand Down
20 changes: 20 additions & 0 deletions docs/solutions/0300-0399/index.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
## 本章内容

- [0300. 最长递增子序列](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/longest-increasing-subsequence.md)
- [0301. 删除无效的括号](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/remove-invalid-parentheses.md)
- [0302. 包含全部黑色像素的最小矩形](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/smallest-rectangle-enclosing-black-pixels.md)
- [0303. 区域和检索 - 数组不可变](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/range-sum-query-immutable.md)
- [0304. 二维区域和检索 - 矩阵不可变](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/range-sum-query-2d-immutable.md)
- [0305. 岛屿数量 II](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/number-of-islands-ii.md)
- [0306. 累加数](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/additive-number.md)
- [0307. 区域和检索 - 数组可修改](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/range-sum-query-mutable.md)
- [0308. 二维区域和检索 - 矩阵可修改](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/range-sum-query-2d-mutable.md)
- [0309. 买卖股票的最佳时机含冷冻期](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/best-time-to-buy-and-sell-stock-with-cooldown.md)
- [0310. 最小高度树](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/minimum-height-trees.md)
- [0311. 稀疏矩阵的乘法](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/sparse-matrix-multiplication.md)
- [0312. 戳气球](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/burst-balloons.md)
- [0313. 超级丑数](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/super-ugly-number.md)
- [0314. 二叉树的垂直遍历](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/binary-tree-vertical-order-traversal.md)
- [0315. 计算右侧小于当前元素的个数](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/count-of-smaller-numbers-after-self.md)
- [0316. 去除重复字母](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/remove-duplicate-letters.md)
- [0317. 离建筑物最近的距离](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/shortest-distance-from-all-buildings.md)
- [0318. 最大单词长度乘积](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/maximum-product-of-word-lengths.md)
- [0319. 灯泡开关](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/bulb-switcher.md)
- [0320. 列举单词的全部缩写](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/generalized-abbreviation.md)
Expand All @@ -23,11 +30,16 @@
- [0327. 区间和的个数](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/count-of-range-sum.md)
- [0328. 奇偶链表](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/odd-even-linked-list.md)
- [0329. 矩阵中的最长递增路径](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/longest-increasing-path-in-a-matrix.md)
- [0330. 按要求补齐数组](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/patching-array.md)
- [0331. 验证二叉树的前序序列化](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/verify-preorder-serialization-of-a-binary-tree.md)
- [0332. 重新安排行程](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/reconstruct-itinerary.md)
- [0333. 最大二叉搜索子树](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/largest-bst-subtree.md)
- [0334. 递增的三元子序列](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/increasing-triplet-subsequence.md)
- [0335. 路径交叉](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/self-crossing.md)
- [0336. 回文对](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/palindrome-pairs.md)
- [0337. 打家劫舍 III](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/house-robber-iii.md)
- [0338. 比特位计数](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/counting-bits.md)
- [0339. 嵌套列表加权和](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/nested-list-weight-sum.md)
- [0340. 至多包含 K 个不同字符的最长子串](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/longest-substring-with-at-most-k-distinct-characters.md)
- [0341. 扁平化嵌套列表迭代器](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/flatten-nested-list-iterator.md)
- [0342. 4的幂](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/power-of-four.md)
Expand All @@ -46,16 +58,21 @@
- [0355. 设计推特](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/design-twitter.md)
- [0356. 直线镜像](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/line-reflection.md)
- [0357. 统计各位数字都不同的数字个数](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/count-numbers-with-unique-digits.md)
- [0358. K 距离间隔重排字符串](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/rearrange-string-k-distance-apart.md)
- [0359. 日志速率限制器](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/logger-rate-limiter.md)
- [0360. 有序转化数组](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/sort-transformed-array.md)
- [0361. 轰炸敌人](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/bomb-enemy.md)
- [0362. 敲击计数器](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/design-hit-counter.md)
- [0363. 矩形区域不超过 K 的最大数值和](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/max-sum-of-rectangle-no-larger-than-k.md)
- [0364. 嵌套列表加权和 II](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/nested-list-weight-sum-ii.md)
- [0365. 水壶问题](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/water-and-jug-problem.md)
- [0366. 寻找二叉树的叶子节点](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/find-leaves-of-binary-tree.md)
- [0367. 有效的完全平方数](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/valid-perfect-square.md)
- [0368. 最大整除子集](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/largest-divisible-subset.md)
- [0369. 给单链表加一](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/plus-one-linked-list.md)
- [0370. 区间加法](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/range-addition.md)
- [0371. 两整数之和](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/sum-of-two-integers.md)
- [0372. 超级次方](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/super-pow.md)
- [0373. 查找和最小的 K 对数字](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/find-k-pairs-with-smallest-sums.md)
- [0374. 猜数字大小](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/guess-number-higher-or-lower.md)
- [0375. 猜数字大小 II](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/guess-number-higher-or-lower-ii.md)
Expand All @@ -76,7 +93,10 @@
- [0390. 消除游戏](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/elimination-game.md)
- [0391. 完美矩形](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/perfect-rectangle.md)
- [0392. 判断子序列](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/is-subsequence.md)
- [0393. UTF-8 编码验证](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/utf-8-validation.md)
- [0394. 字符串解码](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/decode-string.md)
- [0395. 至少有 K 个重复字符的最长子串](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/longest-substring-with-at-least-k-repeating-characters.md)
- [0396. 旋转函数](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/rotate-function.md)
- [0397. 整数替换](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/integer-replacement.md)
- [0398. 随机数索引](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/random-pick-index.md)
- [0399. 除法求值](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/evaluate-division.md)
159 changes: 159 additions & 0 deletions docs/solutions/0300-0399/nested-list-weight-sum-ii.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
# [0364. 嵌套列表加权和 II](https://leetcode.cn/problems/nested-list-weight-sum-ii/)

- 标签:栈、深度优先搜索、广度优先搜索
- 难度:中等

## 题目链接

- [0364. 嵌套列表加权和 II - 力扣](https://leetcode.cn/problems/nested-list-weight-sum-ii/)

## 题目大意

**描述**:

给定一个整数嵌套列表 $nestedList$,每一个元素要么是一个整数,要么是一个列表(这个列表中的每个元素也同样是整数或列表)。

- 整数的「深度」取决于它位于多少个列表内部。例如,嵌套列表 $[1,[2,2],[[3],2],1]$ 的每个整数的值都等于它的深度。令 $maxDepth$ 是任意整数的「最大深度」。
- 整数的「权重」为 $maxDepth$ - (整数的深度) $+ 1$。

**要求**:

将 $nestedList$ 列表中每个整数先乘权重再求和,返回该加权和。

**说明**:

- $1 \le nestedList.length \le 50$。
- 嵌套列表中整数的值在范围 $[-10^{3}, 10^{3}]$。
- 任意整数的最大「深度」小于等于 $50$。
- 没有空列表。

**示例**:

- 示例 1:

![](https://assets.leetcode.com/uploads/2021/03/27/nestedlistweightsumiiex1.png)

```python
输入:nestedList = [[1,1],2,[1,1]]
输出:8
解释:4 个 1 在深度为 1 的位置, 一个 2 在深度为 2 的位置。
1*1 + 1*1 + 2*2 + 1*1 + 1*1 = 8
```

- 示例 2:

![](https://assets.leetcode.com/uploads/2021/03/27/nestedlistweightsumiiex2.png)

```python
输入:nestedList = [1,[4,[6]]]
输出:17
解释:一个 1 在深度为 3 的位置, 一个 4 在深度为 2 的位置,一个 6 在深度为 1 的位置。
1*3 + 4*2 + 6*1 = 17
```

## 解题思路

### 思路 1:两次遍历

1. **第一次遍历**:计算最大深度 $maxDepth$。使用深度优先搜索遍历整个嵌套列表,记录每个整数的深度,并更新最大深度。

2. **第二次遍历**:计算加权和。再次遍历嵌套列表,对于每个整数,其权重为 $maxDepth - depth + 1$,其中 $depth$ 是该整数的深度。将所有整数的值乘以其权重后求和。

具体步骤:

- 定义递归函数 `getMaxDepth(nestedList, depth)` 用于计算最大深度。
- 定义递归函数 `calculateSum(nestedList, depth, maxDepth)` 用于计算加权和。
- 对每个 `NestedInteger` 元素:
- 如果是整数,则在计算最大深度时进行比较和更新;在计算加权和时累加当前值乘以权重。
- 如果是列表,则递归地处理其内部元素。


### 思路 1:代码

```python
# """
# This is the interface that allows for creating nested lists.
# You should not implement it, or speculate about its implementation
# """
#class NestedInteger:
# def __init__(self, value=None):
# """
# If value is not specified, initializes an empty list.
# Otherwise initializes a single integer equal to value.
# """
#
# def isInteger(self):
# """
# @return True if this NestedInteger holds a single integer, rather than a nested list.
# :rtype bool
# """
#
# def add(self, elem):
# """
# Set this NestedInteger to hold a nested list and adds a nested integer elem to it.
# :rtype void
# """
#
# def setInteger(self, value):
# """
# Set this NestedInteger to hold a single integer equal to value.
# :rtype void
# """
#
# def getInteger(self):
# """
# @return the single integer that this NestedInteger holds, if it holds a single integer
# Return None if this NestedInteger holds a nested list
# :rtype int
# """
#
# def getList(self):
# """
# @return the nested list that this NestedInteger holds, if it holds a nested list
# Return None if this NestedInteger holds a single integer
# :rtype List[NestedInteger]
# """

class Solution:
def depthSumInverse(self, nestedList: List[NestedInteger]) -> int:
# 第一次遍历:计算最大深度
max_depth = self.getMaxDepth(nestedList, 1)

# 第二次遍历:计算加权和
return self.calculateSum(nestedList, 1, max_depth)

def getMaxDepth(self, nestedList: List[NestedInteger], depth: int) -> int:
"""计算嵌套列表的最大深度"""
max_depth = depth

for item in nestedList:
if item.isInteger():
# 如果是整数,更新最大深度
max_depth = max(max_depth, depth)
else:
# 如果是列表,递归计算子列表的最大深度
max_depth = max(max_depth, self.getMaxDepth(item.getList(), depth + 1))

return max_depth

def calculateSum(self, nestedList: List[NestedInteger], depth: int, max_depth: int) -> int:
"""计算加权和"""
total_sum = 0

for item in nestedList:
if item.isInteger():
# 如果是整数,计算其加权贡献
# 权重 = max_depth - depth + 1
weight = max_depth - depth + 1
total_sum += item.getInteger() * weight
else:
# 如果是列表,递归计算子列表的加权和
total_sum += self.calculateSum(item.getList(), depth + 1, max_depth)

return total_sum
```

### 思路 1:复杂度分析

- **时间复杂度**:$O(n)$,其中 $n$ 是嵌套列表中所有整数的总数。需要遍历两次嵌套列表,每次遍历的时间复杂度都是 $O(n)$。
- **空间复杂度**:$O(d)$,其中 $d$ 是嵌套列表的最大深度。递归调用栈的深度最多为 $d$。
Loading