Skip to content

Commit 025b3e1

Browse files
committed
更新题解列表
1 parent 12d8dbe commit 025b3e1

File tree

6 files changed

+171
-4
lines changed

6 files changed

+171
-4
lines changed

Contents/00.Introduction/04.Solutions-List.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# LeetCode 题解(已完成 746 道)
1+
# LeetCode 题解(已完成 748 道)
22

33
| 题号 | 标题 | 题解 | 标签 | 难度 |
44
| :------ | :------ | :------ | :------ | :------ |
@@ -192,6 +192,7 @@
192192
| 0238 | [除自身以外数组的乘积](https://leetcode.cn/problems/product-of-array-except-self/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0238.%20%E9%99%A4%E8%87%AA%E8%BA%AB%E4%BB%A5%E5%A4%96%E6%95%B0%E7%BB%84%E7%9A%84%E4%B9%98%E7%A7%AF.md) | 数组 | 中等 |
193193
| 0239 | [滑动窗口最大值](https://leetcode.cn/problems/sliding-window-maximum/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0239.%20%E6%BB%91%E5%8A%A8%E7%AA%97%E5%8F%A3%E6%9C%80%E5%A4%A7%E5%80%BC.md) | 队列,数组、滑动窗口、单调队列、堆(优先队列) | 困难 |
194194
| 0240 | [搜索二维矩阵 II](https://leetcode.cn/problems/search-a-2d-matrix-ii/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0240.%20%E6%90%9C%E7%B4%A2%E4%BA%8C%E7%BB%B4%E7%9F%A9%E9%98%B5%20II.md) | 二分查找、分治算法 | 中等 |
195+
| 0241 | [为运算表达式设计优先级](https://leetcode.cn/problems/different-ways-to-add-parentheses/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0241.%20%E4%B8%BA%E8%BF%90%E7%AE%97%E8%A1%A8%E8%BE%BE%E5%BC%8F%E8%AE%BE%E8%AE%A1%E4%BC%98%E5%85%88%E7%BA%A7.md) | 递归、记忆化搜索、数学、字符串、动态规划 | 中等 |
195196
| 0242 | [有效的字母异位词](https://leetcode.cn/problems/valid-anagram/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0242.%20%E6%9C%89%E6%95%88%E7%9A%84%E5%AD%97%E6%AF%8D%E5%BC%82%E4%BD%8D%E8%AF%8D.md) | 字符串、哈希表、排序 | 简单 |
196197
| 0249 | [移位字符串分组](https://leetcode.cn/problems/group-shifted-strings/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0249.%20%E7%A7%BB%E4%BD%8D%E5%AD%97%E7%AC%A6%E4%B8%B2%E5%88%86%E7%BB%84.md) | 哈希表、字符串 | 中等 |
197198
| 0257 | [二叉树的所有路径](https://leetcode.cn/problems/binary-tree-paths/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0257.%20%E4%BA%8C%E5%8F%89%E6%A0%91%E7%9A%84%E6%89%80%E6%9C%89%E8%B7%AF%E5%BE%84.md) | 树、深度优先搜索 | 简单 |
@@ -439,6 +440,7 @@
439440
| 0919 | [完全二叉树插入器](https://leetcode.cn/problems/complete-binary-tree-inserter/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0919.%20%E5%AE%8C%E5%85%A8%E4%BA%8C%E5%8F%89%E6%A0%91%E6%8F%92%E5%85%A5%E5%99%A8.md) | 树、广度优先搜索、设计、二叉树 | 中等 |
440441
| 0921 | [使括号有效的最少添加](https://leetcode.cn/problems/minimum-add-to-make-parentheses-valid/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0921.%20%E4%BD%BF%E6%8B%AC%E5%8F%B7%E6%9C%89%E6%95%88%E7%9A%84%E6%9C%80%E5%B0%91%E6%B7%BB%E5%8A%A0.md) | 栈、贪心、字符串 | 中等 |
441442
| 0925 | [长按键入](https://leetcode.cn/problems/long-pressed-name/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0925.%20%E9%95%BF%E6%8C%89%E9%94%AE%E5%85%A5.md) | 双指针、字符串 | 简单 |
443+
| 0932 | [漂亮数组](https://leetcode.cn/problems/beautiful-array) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0932.%20%E6%BC%82%E4%BA%AE%E6%95%B0%E7%BB%84.md) | 数组、数学、分治 | 中等 |
442444
| 0933 | [最近的请求次数](https://leetcode.cn/problems/number-of-recent-calls/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0933.%20%E6%9C%80%E8%BF%91%E7%9A%84%E8%AF%B7%E6%B1%82%E6%AC%A1%E6%95%B0.md) | 设计、队列、数据流 | 简单 |
443445
| 0938 | [二叉搜索树的范围和](https://leetcode.cn/problems/range-sum-of-bst/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0938.%20%E4%BA%8C%E5%8F%89%E6%90%9C%E7%B4%A2%E6%A0%91%E7%9A%84%E8%8C%83%E5%9B%B4%E5%92%8C.md) | 二叉树 | 简单 |
444446
| 0946 | [验证栈序列](https://leetcode.cn/problems/validate-stack-sequences/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0946.%20%E9%AA%8C%E8%AF%81%E6%A0%88%E5%BA%8F%E5%88%97.md) | 栈、数组、模拟 | 中等 |

Contents/00.Introduction/05.Categories-List.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -741,7 +741,7 @@
741741
| 0004 | [寻找两个正序数组的中位数](https://leetcode.cn/problems/median-of-two-sorted-arrays/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0004.%20%E5%AF%BB%E6%89%BE%E4%B8%A4%E4%B8%AA%E6%AD%A3%E5%BA%8F%E6%95%B0%E7%BB%84%E7%9A%84%E4%B8%AD%E4%BD%8D%E6%95%B0.md) | 数组、二分查找、分治算法 | 困难 |
742742
| 0023 | [合并K个升序链表](https://leetcode.cn/problems/merge-k-sorted-lists/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0023.%20%E5%90%88%E5%B9%B6K%E4%B8%AA%E5%8D%87%E5%BA%8F%E9%93%BE%E8%A1%A8.md) | 链表、分治、堆(优先队列)、归并排序 | 困难 |
743743
| 0053 | [最大子数组和](https://leetcode.cn/problems/maximum-subarray/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0053.%20%E6%9C%80%E5%A4%A7%E5%AD%90%E6%95%B0%E7%BB%84%E5%92%8C.md) | 数组、分治算法、动态规划 | 简单 |
744-
| 0241 | 为运算表达式设计优先级 | | | |
744+
| 0241 | [为运算表达式设计优先级](https://leetcode.cn/problems/different-ways-to-add-parentheses/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0241.%20%E4%B8%BA%E8%BF%90%E7%AE%97%E8%A1%A8%E8%BE%BE%E5%BC%8F%E8%AE%BE%E8%AE%A1%E4%BC%98%E5%85%88%E7%BA%A7.md) | 递归、记忆化搜索、数学、字符串、动态规划 | 中等 |
745745
| 0169 | [多数元素](https://leetcode.cn/problems/majority-element/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0169.%20%E5%A4%9A%E6%95%B0%E5%85%83%E7%B4%A0.md) | 数组、哈希表 | 简单 |
746746
| 0050 | [Pow(x, n)](https://leetcode.cn/problems/powx-n/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0050.%20Pow%28x%2C%20n%29.md) | 数学、二分查找 | 中等 |
747747
| 0014 | [最长公共前缀](https://leetcode.cn/problems/longest-common-prefix/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0014.%20%E6%9C%80%E9%95%BF%E5%85%AC%E5%85%B1%E5%89%8D%E7%BC%80.md) | 字符串 | 简单 |

Contents/09.Algorithm-Base/03.Divide-And-Conquer-Algorithm/02.Divide-And-Conquer-Algorithm-List.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
| 0004 | [寻找两个正序数组的中位数](https://leetcode.cn/problems/median-of-two-sorted-arrays/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0004.%20%E5%AF%BB%E6%89%BE%E4%B8%A4%E4%B8%AA%E6%AD%A3%E5%BA%8F%E6%95%B0%E7%BB%84%E7%9A%84%E4%B8%AD%E4%BD%8D%E6%95%B0.md) | 数组、二分查找、分治算法 | 困难 |
66
| 0023 | [合并K个升序链表](https://leetcode.cn/problems/merge-k-sorted-lists/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0023.%20%E5%90%88%E5%B9%B6K%E4%B8%AA%E5%8D%87%E5%BA%8F%E9%93%BE%E8%A1%A8.md) | 链表、分治、堆(优先队列)、归并排序 | 困难 |
77
| 0053 | [最大子数组和](https://leetcode.cn/problems/maximum-subarray/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0053.%20%E6%9C%80%E5%A4%A7%E5%AD%90%E6%95%B0%E7%BB%84%E5%92%8C.md) | 数组、分治算法、动态规划 | 简单 |
8-
| 0241 | 为运算表达式设计优先级 | | | |
8+
| 0241 | [为运算表达式设计优先级](https://leetcode.cn/problems/different-ways-to-add-parentheses/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0241.%20%E4%B8%BA%E8%BF%90%E7%AE%97%E8%A1%A8%E8%BE%BE%E5%BC%8F%E8%AE%BE%E8%AE%A1%E4%BC%98%E5%85%88%E7%BA%A7.md) | 递归、记忆化搜索、数学、字符串、动态规划 | 中等 |
99
| 0169 | [多数元素](https://leetcode.cn/problems/majority-element/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0169.%20%E5%A4%9A%E6%95%B0%E5%85%83%E7%B4%A0.md) | 数组、哈希表 | 简单 |
1010
| 0050 | [Pow(x, n)](https://leetcode.cn/problems/powx-n/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0050.%20Pow%28x%2C%20n%29.md) | 数学、二分查找 | 中等 |
1111
| 0014 | [最长公共前缀](https://leetcode.cn/problems/longest-common-prefix/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0014.%20%E6%9C%80%E9%95%BF%E5%85%AC%E5%85%B1%E5%89%8D%E7%BC%80.md) | 字符串 | 简单 |

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,4 +254,4 @@
254254
- [动态规划优化题目](./Contents/10.Dynamic-Programming/11.DP-Optimization/04.DP-Optimization-List.md)
255255

256256
## 11. 附加内容
257-
## [12. LeetCode 题解(已完成 746 道)](./Contents/00.Introduction/04.Solutions-List.md)
257+
## [12. LeetCode 题解(已完成 748 道)](./Contents/00.Introduction/04.Solutions-List.md)
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# [0241. 为运算表达式设计优先级](https://leetcode.cn/problems/different-ways-to-add-parentheses/)
2+
3+
- 标签:递归、记忆化搜索、数学、字符串、动态规划
4+
- 难度:中等
5+
6+
## 题目大意
7+
8+
**描述**:给定一个由数字和运算符组成的字符串 `expression`
9+
10+
**要求**:按不同优先级组合数字和运算符,计算并返回所有可能组合的结果。你可以按任意顺序返回答案。
11+
12+
**说明**
13+
14+
- 生成的测试用例满足其对应输出值符合 $32$ 位整数范围,不同结果的数量不超过 $10^4$。
15+
- $1 \le expression.length \le 20$。
16+
- `expression` 由数字和算符 `'+'``'-'``'*'` 组成。
17+
- 输入表达式中的所有整数值在范围 $[0, 99]$。
18+
19+
**示例**
20+
21+
- 示例 1:
22+
23+
```Python
24+
输入:expression = "2-1-1"
25+
输出:[0,2]
26+
解释:
27+
((2-1)-1) = 0
28+
(2-(1-1)) = 2
29+
```
30+
31+
- 示例 2:
32+
33+
```Python
34+
输入:expression = "2*3-4*5"
35+
输出:[-34,-14,-10,-10,10]
36+
解释:
37+
(2*(3-(4*5))) = -34
38+
((2*3)-(4*5)) = -14
39+
((2*(3-4))*5) = -10
40+
(2*((3-4)*5)) = -10
41+
(((2*3)-4)*5) = 10
42+
```
43+
44+
## 解题思路
45+
46+
### 思路 1:分治算法
47+
48+
给定的字符串 `expression` 只包含有数字和字符,可以写成类似 `x op y` 的形式,其中 $x$、$y$ 为表达式或数字,$op$ 为字符。
49+
50+
则我们可以根据字符的位置,将其递归分解为 $x$、$y$ 两个部分,接着分别计算 $x$ 部分的结果与 $y$ 部分的结果。然后再将其合并。
51+
52+
### 思路 1:代码
53+
54+
```Python
55+
class Solution:
56+
def diffWaysToCompute(self, expression: str) -> List[int]:
57+
res = []
58+
if len(expression) <= 2:
59+
res.append(int(expression))
60+
return res
61+
62+
for i in range(len(expression)):
63+
ch = expression[i]
64+
if ch == '+' or ch == '-' or ch == '*':
65+
left_cnts = self.diffWaysToCompute(expression[ :i])
66+
right_cnts = self.diffWaysToCompute(expression[i + 1:])
67+
68+
for left in left_cnts:
69+
for right in right_cnts:
70+
if ch == '+':
71+
res.append(left + right)
72+
elif ch == '-':
73+
res.append(left - right)
74+
else:
75+
res.append(left * right)
76+
77+
return res
78+
```
79+
80+
### 思路 1:复杂度分析
81+
82+
- **时间复杂度**:$O(C_n)$,其中 $n$ 为结果数组的大小,$C_n$ 是第 $k$ 个卡特兰数。
83+
- **空间复杂度**:$O(C_n)$。

Solutions/0932. 漂亮数组.md

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# [0932. 漂亮数组](https://leetcode.cn/problems/beautiful-array)
2+
3+
- 标签:数组、数学、分治
4+
- 难度:中等
5+
6+
## 题目大意
7+
8+
**描述**:给定一个整数 $n$。
9+
10+
**要求**:返回长度为 $n$ 的任一漂亮数组。
11+
12+
**说明**
13+
14+
- **漂亮数组**(长度为 $n$ 的数组 $nums$ 满足下述条件):
15+
- $nums$ 是由范围 $[1, n]$ 的整数组成的一个排列。
16+
- 对于每个 $0 \le i < j < n$,均不存在下标 $k$($i < k < j$)使得 $2 \times nums[k] == nums[i] + nums[j]$。
17+
- $1 \le n \le 1000$。
18+
- 本题保证对于给定的 $n$ 至少存在一个有效答案。
19+
20+
**示例**
21+
22+
- 示例 1:
23+
24+
```Python
25+
输入:n = 4
26+
输出:[2,1,4,3]
27+
```
28+
29+
- 示例 2:
30+
31+
```Python
32+
输入:n = 5
33+
输出:[3,1,2,5,4]
34+
```
35+
36+
## 解题思路
37+
38+
### 思路 1:分治算法
39+
40+
根据题目要求,我们可以得到以下信息:
41+
42+
1. 题目要求 $2 \times nums[k] == nums[i] + nums[j], (0 \le i < k < j < n)$ 不能成立,可知:等式左侧必为偶数,只要右侧和为奇数则等式不成立。
43+
2. 已知:奇数 + 偶数 = 奇数,则令 $nums[i]$ 和 $nums[j]$ 其中一个为奇数,另一个为偶数,即可保证 $nums[i] + nums[j]$ 一定为奇数。这里我们不妨令 $nums[i]$ 为奇数,令 $nums[j]$ 为偶数。
44+
3. 如果数组 $nums$ 是漂亮数组,那么对数组 $nums$ 的每一位元素乘以一个常数或者加上一个常数之后,$nums$ 仍是漂亮数组。
45+
- 即如果 $[a_1, a_2, ..., a_n]$ 是一个漂亮数组,那么 $[k \times a_1 + b, k \times a_2 + b, ..., k \times a_n + b]$ 也是漂亮数组。
46+
47+
那么,我们可以按照下面的规则构建长度为 $n$ 的漂亮数组。
48+
49+
1. 当 $n = 1$ 时,返回 $[1]$。此时数组 $nums$ 中仅有 $1$ 个元素,并且满足漂亮数组的条件。
50+
2. 当 $n > 1$ 时,我们将 $nums$ 分解为左右两个部分:`left_nums``right_nums`。如果左右两个部分满足:
51+
1. 数组 `left_nums` 中元素全为奇数(可以通过 `nums[i] * 2 - 1``left_nums` 中元素全部映射为奇数)。
52+
2. 数组 `right_nums` 中元素全为偶数(可以通过 `nums[i] * 2``right_nums` 中元素全部映射为偶数)。
53+
3. `left_nums``right_nums` 都是漂亮数组。
54+
3. 那么 `left_nums + right_nums` 构成的数组一定也是漂亮数组,即 $nums$ 为漂亮数组,将 $nums$ 返回即可。
55+
56+
### 思路 1:代码
57+
58+
```Python
59+
class Solution:
60+
def beautifulArray(self, n: int) -> List[int]:
61+
if n == 1:
62+
return [1]
63+
64+
nums = [0 for _ in range(n)]
65+
left_cnt = (n + 1) // 2
66+
right_cnt = n - left_cnt
67+
left_nums = self.beautifulArray(left_cnt)
68+
right_nums = self.beautifulArray(right_cnt)
69+
70+
for i in range(left_cnt):
71+
nums[i] = 2 * left_nums[i] - 1
72+
73+
for i in range(right_cnt):
74+
nums[left_cnt + i] = 2 * right_nums[i]
75+
76+
return nums
77+
```
78+
79+
### 思路 1:复杂度分析
80+
81+
- **时间复杂度**:$O(n \times \log n)$,其中 $n$ 为数组 $nums$ 的长度。
82+
- **空间复杂度**:$O(n \times \log n)$。

0 commit comments

Comments
 (0)