Skip to content

Commit

Permalink
更新题解列表
Browse files Browse the repository at this point in the history
  • Loading branch information
itcharge committed Feb 28, 2023
1 parent 12d8dbe commit 025b3e1
Show file tree
Hide file tree
Showing 6 changed files with 171 additions and 4 deletions.
4 changes: 3 additions & 1 deletion Contents/00.Introduction/04.Solutions-List.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# LeetCode 题解(已完成 746 道)
# LeetCode 题解(已完成 748 道)

| 题号 | 标题 | 题解 | 标签 | 难度 |
| :------ | :------ | :------ | :------ | :------ |
Expand Down Expand Up @@ -192,6 +192,7 @@
| 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) | 数组 | 中等 |
| 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) | 队列,数组、滑动窗口、单调队列、堆(优先队列) | 困难 |
| 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) | 二分查找、分治算法 | 中等 |
| 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) | 递归、记忆化搜索、数学、字符串、动态规划 | 中等 |
| 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) | 字符串、哈希表、排序 | 简单 |
| 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) | 哈希表、字符串 | 中等 |
| 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) | 树、深度优先搜索 | 简单 |
Expand Down Expand Up @@ -439,6 +440,7 @@
| 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) | 树、广度优先搜索、设计、二叉树 | 中等 |
| 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) | 栈、贪心、字符串 | 中等 |
| 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) | 双指针、字符串 | 简单 |
| 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) | 数组、数学、分治 | 中等 |
| 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) | 设计、队列、数据流 | 简单 |
| 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) | 二叉树 | 简单 |
| 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) | 栈、数组、模拟 | 中等 |
Expand Down
2 changes: 1 addition & 1 deletion Contents/00.Introduction/05.Categories-List.md
Original file line number Diff line number Diff line change
Expand Up @@ -741,7 +741,7 @@
| 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) | 数组、二分查找、分治算法 | 困难 |
| 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) | 链表、分治、堆(优先队列)、归并排序 | 困难 |
| 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) | 数组、分治算法、动态规划 | 简单 |
| 0241 | 为运算表达式设计优先级 | | | |
| 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) | 递归、记忆化搜索、数学、字符串、动态规划 | 中等 |
| 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) | 数组、哈希表 | 简单 |
| 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) | 数学、二分查找 | 中等 |
| 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) | 字符串 | 简单 |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
| 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) | 数组、二分查找、分治算法 | 困难 |
| 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) | 链表、分治、堆(优先队列)、归并排序 | 困难 |
| 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) | 数组、分治算法、动态规划 | 简单 |
| 0241 | 为运算表达式设计优先级 | | | |
| 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) | 递归、记忆化搜索、数学、字符串、动态规划 | 中等 |
| 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) | 数组、哈希表 | 简单 |
| 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) | 数学、二分查找 | 中等 |
| 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) | 字符串 | 简单 |
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -254,4 +254,4 @@
- [动态规划优化题目](./Contents/10.Dynamic-Programming/11.DP-Optimization/04.DP-Optimization-List.md)

## 11. 附加内容
## [12. LeetCode 题解(已完成 746 道)](./Contents/00.Introduction/04.Solutions-List.md)
## [12. LeetCode 题解(已完成 748 道)](./Contents/00.Introduction/04.Solutions-List.md)
83 changes: 83 additions & 0 deletions Solutions/0241. 为运算表达式设计优先级.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# [0241. 为运算表达式设计优先级](https://leetcode.cn/problems/different-ways-to-add-parentheses/)

- 标签:递归、记忆化搜索、数学、字符串、动态规划
- 难度:中等

## 题目大意

**描述**:给定一个由数字和运算符组成的字符串 `expression`

**要求**:按不同优先级组合数字和运算符,计算并返回所有可能组合的结果。你可以按任意顺序返回答案。

**说明**

- 生成的测试用例满足其对应输出值符合 $32$ 位整数范围,不同结果的数量不超过 $10^4$。
- $1 \le expression.length \le 20$。
- `expression` 由数字和算符 `'+'``'-'``'*'` 组成。
- 输入表达式中的所有整数值在范围 $[0, 99]$。

**示例**

- 示例 1:

```Python
输入:expression = "2-1-1"
输出:[0,2]
解释:
((2-1)-1) = 0
(2-(1-1)) = 2
```

- 示例 2:

```Python
输入:expression = "2*3-4*5"
输出:[-34,-14,-10,-10,10]
解释:
(2*(3-(4*5))) = -34
((2*3)-(4*5)) = -14
((2*(3-4))*5) = -10
(2*((3-4)*5)) = -10
(((2*3)-4)*5) = 10
```

## 解题思路

### 思路 1:分治算法

给定的字符串 `expression` 只包含有数字和字符,可以写成类似 `x op y` 的形式,其中 $x$、$y$ 为表达式或数字,$op$ 为字符。

则我们可以根据字符的位置,将其递归分解为 $x$、$y$ 两个部分,接着分别计算 $x$ 部分的结果与 $y$ 部分的结果。然后再将其合并。

### 思路 1:代码

```Python
class Solution:
def diffWaysToCompute(self, expression: str) -> List[int]:
res = []
if len(expression) <= 2:
res.append(int(expression))
return res

for i in range(len(expression)):
ch = expression[i]
if ch == '+' or ch == '-' or ch == '*':
left_cnts = self.diffWaysToCompute(expression[ :i])
right_cnts = self.diffWaysToCompute(expression[i + 1:])

for left in left_cnts:
for right in right_cnts:
if ch == '+':
res.append(left + right)
elif ch == '-':
res.append(left - right)
else:
res.append(left * right)

return res
```

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

- **时间复杂度**:$O(C_n)$,其中 $n$ 为结果数组的大小,$C_n$ 是第 $k$ 个卡特兰数。
- **空间复杂度**:$O(C_n)$。
82 changes: 82 additions & 0 deletions Solutions/0932. 漂亮数组.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
# [0932. 漂亮数组](https://leetcode.cn/problems/beautiful-array)

- 标签:数组、数学、分治
- 难度:中等

## 题目大意

**描述**:给定一个整数 $n$。

**要求**:返回长度为 $n$ 的任一漂亮数组。

**说明**

- **漂亮数组**(长度为 $n$ 的数组 $nums$ 满足下述条件):
- $nums$ 是由范围 $[1, n]$ 的整数组成的一个排列。
- 对于每个 $0 \le i < j < n$,均不存在下标 $k$($i < k < j$)使得 $2 \times nums[k] == nums[i] + nums[j]$。
- $1 \le n \le 1000$。
- 本题保证对于给定的 $n$ 至少存在一个有效答案。

**示例**

- 示例 1:

```Python
输入:n = 4
输出:[2,1,4,3]
```

- 示例 2:

```Python
输入:n = 5
输出:[3,1,2,5,4]
```

## 解题思路

### 思路 1:分治算法

根据题目要求,我们可以得到以下信息:

1. 题目要求 $2 \times nums[k] == nums[i] + nums[j], (0 \le i < k < j < n)$ 不能成立,可知:等式左侧必为偶数,只要右侧和为奇数则等式不成立。
2. 已知:奇数 + 偶数 = 奇数,则令 $nums[i]$ 和 $nums[j]$ 其中一个为奇数,另一个为偶数,即可保证 $nums[i] + nums[j]$ 一定为奇数。这里我们不妨令 $nums[i]$ 为奇数,令 $nums[j]$ 为偶数。
3. 如果数组 $nums$ 是漂亮数组,那么对数组 $nums$ 的每一位元素乘以一个常数或者加上一个常数之后,$nums$ 仍是漂亮数组。
- 即如果 $[a_1, a_2, ..., a_n]$ 是一个漂亮数组,那么 $[k \times a_1 + b, k \times a_2 + b, ..., k \times a_n + b]$ 也是漂亮数组。

那么,我们可以按照下面的规则构建长度为 $n$ 的漂亮数组。

1. 当 $n = 1$ 时,返回 $[1]$。此时数组 $nums$ 中仅有 $1$ 个元素,并且满足漂亮数组的条件。
2. 当 $n > 1$ 时,我们将 $nums$ 分解为左右两个部分:`left_nums``right_nums`。如果左右两个部分满足:
1. 数组 `left_nums` 中元素全为奇数(可以通过 `nums[i] * 2 - 1``left_nums` 中元素全部映射为奇数)。
2. 数组 `right_nums` 中元素全为偶数(可以通过 `nums[i] * 2``right_nums` 中元素全部映射为偶数)。
3. `left_nums``right_nums` 都是漂亮数组。
3. 那么 `left_nums + right_nums` 构成的数组一定也是漂亮数组,即 $nums$ 为漂亮数组,将 $nums$ 返回即可。

### 思路 1:代码

```Python
class Solution:
def beautifulArray(self, n: int) -> List[int]:
if n == 1:
return [1]

nums = [0 for _ in range(n)]
left_cnt = (n + 1) // 2
right_cnt = n - left_cnt
left_nums = self.beautifulArray(left_cnt)
right_nums = self.beautifulArray(right_cnt)

for i in range(left_cnt):
nums[i] = 2 * left_nums[i] - 1

for i in range(right_cnt):
nums[left_cnt + i] = 2 * right_nums[i]

return nums
```

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

- **时间复杂度**:$O(n \times \log n)$,其中 $n$ 为数组 $nums$ 的长度。
- **空间复杂度**:$O(n \times \log n)$。

0 comments on commit 025b3e1

Please sign in to comment.