Skip to content

Commit 0209deb

Browse files
committed
更新题解列表
1 parent f819b24 commit 0209deb

File tree

5 files changed

+220
-13
lines changed

5 files changed

+220
-13
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 题解(已完成 832 道)
1+
# LeetCode 题解(已完成 834 道)
22

33
| 题号 | 标题 | 题解 | 标签 | 难度 |
44
| :------ | :------ | :------ | :------ | :------ |
@@ -499,6 +499,7 @@
499499
| 1012 | [至少有 1 位重复的数字](https://leetcode.cn/problems/numbers-with-repeated-digits/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1012.%20%E8%87%B3%E5%B0%91%E6%9C%89%201%20%E4%BD%8D%E9%87%8D%E5%A4%8D%E7%9A%84%E6%95%B0%E5%AD%97.md) | 数学、动态规划 | 困难 |
500500
| 1014 | [最佳观光组合](https://leetcode.cn/problems/best-sightseeing-pair/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1014.%20%E6%9C%80%E4%BD%B3%E8%A7%82%E5%85%89%E7%BB%84%E5%90%88.md) | 数组、动态规划 | 中等 |
501501
| 1020 | [飞地的数量](https://leetcode.cn/problems/number-of-enclaves/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1020.%20%E9%A3%9E%E5%9C%B0%E7%9A%84%E6%95%B0%E9%87%8F.md) | 深度优先搜索、广度优先搜索、并查集、数组、矩阵 | 中等 |
502+
| 1021 | [删除最外层的括号](https://leetcode.cn/problems/remove-outermost-parentheses/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1021.%20%E5%88%A0%E9%99%A4%E6%9C%80%E5%A4%96%E5%B1%82%E7%9A%84%E6%8B%AC%E5%8F%B7.md) | 栈、字符串 | 简单 |
502503
| 1023 | [驼峰式匹配](https://leetcode.cn/problems/camelcase-matching/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1023.%20%E9%A9%BC%E5%B3%B0%E5%BC%8F%E5%8C%B9%E9%85%8D.md) | 字典树、双指针、字符串、字符串匹配 | 中等 |
503504
| 1025 | [除数博弈](https://leetcode.cn/problems/divisor-game/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1025.%20%E9%99%A4%E6%95%B0%E5%8D%9A%E5%BC%88.md) | 脑筋急转弯、数学、动态规划、博弈 | 简单 |
504505
| 1028 | [从先序遍历还原二叉树](https://leetcode.cn/problems/recover-a-tree-from-preorder-traversal/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1028.%20%E4%BB%8E%E5%85%88%E5%BA%8F%E9%81%8D%E5%8E%86%E8%BF%98%E5%8E%9F%E4%BA%8C%E5%8F%89%E6%A0%91.md) | 树、深度优先搜索、字符串、二叉树 | 困难 |
@@ -602,6 +603,7 @@
602603
| 1790 | [仅执行一次字符串交换能否使两个字符串相等](https://leetcode.cn/problems/check-if-one-string-swap-can-make-strings-equal/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1790.%20%E4%BB%85%E6%89%A7%E8%A1%8C%E4%B8%80%E6%AC%A1%E5%AD%97%E7%AC%A6%E4%B8%B2%E4%BA%A4%E6%8D%A2%E8%83%BD%E5%90%A6%E4%BD%BF%E4%B8%A4%E4%B8%AA%E5%AD%97%E7%AC%A6%E4%B8%B2%E7%9B%B8%E7%AD%89.md) | 哈希表、字符串、计数 | 简单 |
603604
| 1791 | [找出星型图的中心节点](https://leetcode.cn/problems/find-center-of-star-graph/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1791.%20%E6%89%BE%E5%87%BA%E6%98%9F%E5%9E%8B%E5%9B%BE%E7%9A%84%E4%B8%AD%E5%BF%83%E8%8A%82%E7%82%B9.md) || 简单 |
604605
| 1822 | [数组元素积的符号](https://leetcode.cn/problems/sign-of-the-product-of-an-array/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1822.%20%E6%95%B0%E7%BB%84%E5%85%83%E7%B4%A0%E7%A7%AF%E7%9A%84%E7%AC%A6%E5%8F%B7.md) | 数组、数学 | 简单 |
606+
| 1827 | [最少操作使数组递增](https://leetcode.cn/problems/minimum-operations-to-make-the-array-increasing/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1827.%20%E6%9C%80%E5%B0%91%E6%93%8D%E4%BD%9C%E4%BD%BF%E6%95%B0%E7%BB%84%E9%80%92%E5%A2%9E.md) | 贪心、数组 | 简单 |
605607
| 1833 | [雪糕的最大数量](https://leetcode.cn/problems/maximum-ice-cream-bars/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1833.%20%E9%9B%AA%E7%B3%95%E7%9A%84%E6%9C%80%E5%A4%A7%E6%95%B0%E9%87%8F.md) | 贪心、数组、排序 | 中等 |
606608
| 1844 | [将所有数字用字符替换](https://leetcode.cn/problems/replace-all-digits-with-characters/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1844.%20%E5%B0%86%E6%89%80%E6%9C%89%E6%95%B0%E5%AD%97%E7%94%A8%E5%AD%97%E7%AC%A6%E6%9B%BF%E6%8D%A2.md) | 字符串 | 简单 |
607609
| 1858 | [包含所有前缀的最长单词](https://leetcode.cn/problems/longest-word-with-all-prefixes/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1858.%20%E5%8C%85%E5%90%AB%E6%89%80%E6%9C%89%E5%89%8D%E7%BC%80%E7%9A%84%E6%9C%80%E9%95%BF%E5%8D%95%E8%AF%8D.md) | 深度优先搜索、字典树 | 中等 |

README.md

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

257257
## 11. 附加内容
258-
## [12. LeetCode 题解(已完成 832 道)](./Contents/00.Introduction/04.Solutions-List.md)
258+
## [12. LeetCode 题解(已完成 834 道)](./Contents/00.Introduction/04.Solutions-List.md)

Solutions/0487. 最大连续1的个数 II.md

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,49 @@
99

1010
## 题目大意
1111

12-
给定一个二进制数组,可以最多将 `1``0` 翻转为 `1`
12+
**描述**给定一个二进制数组 $nums$,可以最多将 $1$$0$ 翻转为 $1$
1313

14-
要求:找出其中最大连续 `1` 的个数。
14+
**要求**:如果最多可以翻转一个 $0$,则返回数组中连续 $1$ 的最大个数。
15+
16+
**说明**
17+
18+
- 1 <= nums.length <= 105
19+
nums[i] 不是 0 就是 1.
20+
21+
**示例**
22+
23+
- 示例 1:
24+
25+
```python
26+
输入:nums = [1,0,1,1,0]
27+
输出:4
28+
解释:翻转第一个 0 可以得到最长的连续 1。当翻转以后,最大连续 1 的个数为 4
29+
```
30+
31+
- 示例 2:
32+
33+
```python
34+
输入:nums = [1,0,1,1,0,1]
35+
输出:4
36+
```
1537

1638
## 解题思路
1739

18-
暴力做法是尝试将每个位置的 `0` 分别变为 `1`,然后统计最大连续 `1` 的个数。但这样复杂度就太高了。
40+
### 思路 1:滑动窗口
1941

20-
我们可以使用滑动窗口来解决问题。保证滑动窗口内最多有 `1``0`。具体做法如下:
42+
暴力做法是尝试将每个位置的 $0$ 分别变为 $1$,然后统计最大连续 $1$ 的个数。但这样复杂度就太高了。
2143

22-
设定两个指针:`left``right`,分别指向滑动窗口的左右边界,保证滑动窗口内最多有 `1``0`。使用 `zero_count` 统计窗口内 `1` 的个数。使用 `ans` 记录答案。
44+
我们可以使用滑动窗口来解决问题。保证滑动窗口内最多有 $1$$0$。具体做法如下:
2345

24-
- 一开始,`left``right` 都指向 `0`
25-
- 如果 `nums[right] == 0`,则窗口内 `1` 的个数 + 1。
26-
- 如果该窗口中 `1` 的个数多于 `1` 个,即 `zero_count > 1`,则不断右移 `left`,缩小滑动窗口长度,并更新窗口中 `1` 的个数,直到 `zero_count <= 1`
27-
- 维护更新最大连续 `1` 的个数。然后右移 `right`,直到 `right >= len(nums)` 结束。
28-
- 输出最大连续 `1` 的个数。
46+
设定两个指针:$left$、$right$,分别指向滑动窗口的左右边界,保证滑动窗口内最多有 $1$ 个 $0$。使用 $zero\underline{}count$ 统计窗口内 $1$ 的个数。使用 $ans$ 记录答案。
2947

30-
## 代码
48+
- 一开始,$left$、$right$ 都指向 $0$。
49+
- 如果 $nums[right] == 0$,则窗口内 $1$ 的个数加 $1$。
50+
- 如果该窗口中 $1$ 的个数多于 $1$ 个,即 $zero\underline{}count > 1$,则不断右移 $left$,缩小滑动窗口长度,并更新窗口中 $1$ 的个数,直到 $zero\underline{}count \le 1$。
51+
- 维护更新最大连续 $1$ 的个数。然后右移 $right$,直到 $right \ge len(nums)$ 结束。
52+
- 输出最大连续 $1$ 的个数。
53+
54+
### 思路 1:代码
3155

3256
```python
3357
class Solution:
@@ -49,3 +73,8 @@ class Solution:
4973
return ans
5074
```
5175

76+
### 思路 1:复杂度分析
77+
78+
- **时间复杂度**:$O(n)$,其中 $n$ 为数组 $nums$ 的长度。
79+
- **空间复杂度**:$O(1)$。
80+
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
# [1021. 删除最外层的括号](https://leetcode.cn/problems/remove-outermost-parentheses/)
2+
3+
- 标签:栈、字符串
4+
- 难度:简单
5+
6+
## 题目链接
7+
8+
- [1021. 删除最外层的括号 - 力扣](https://leetcode.cn/problems/remove-outermost-parentheses/)
9+
10+
## 题目大意
11+
12+
**描述**:有效括号字符串为空 `""``"("` + $A$ + `")"` 或 $A + B$ ,其中 $A$ 和 $B$ 都是有效的括号字符串,$+$ 代表字符串的连接。
13+
14+
- 例如,`""``"()"``"(())()"``"(()(()))"` 都是有效的括号字符串。
15+
16+
如果有效字符串 $s$ 非空,且不存在将其拆分为 $s = A + B$ 的方法,我们称其为原语(primitive),其中 $A$ 和 $B$ 都是非空有效括号字符串。
17+
18+
给定一个非空有效字符串 $s$,考虑将其进行原语化分解,使得:$s = P_1 + P_2 + ... + P_k$,其中 $P_i$ 是有效括号字符串原语。
19+
20+
**要求**:对 $s$ 进行原语化分解,删除分解中每个原语字符串的最外层括号,返回 $s$。
21+
22+
**说明**
23+
24+
- $1 \le s.length \le 10^5$。
25+
- $s[i]$ 为 `'('``')'`
26+
- $s$ 是一个有效括号字符串。
27+
28+
**示例**
29+
30+
- 示例 1:
31+
32+
```python
33+
输入:s = "(()())(())"
34+
输出:"()()()"
35+
解释:
36+
输入字符串为 "(()())(())",原语化分解得到 "(()())" + "(())"
37+
删除每个部分中的最外层括号后得到 "()()" + "()" = "()()()"
38+
```
39+
40+
- 示例 2:
41+
42+
```python
43+
输入:s = "(()())(())(()(()))"
44+
输出:"()()()()(())"
45+
解释:
46+
输入字符串为 "(()())(())(()(()))",原语化分解得到 "(()())" + "(())" + "(()(()))"
47+
删除每个部分中的最外层括号后得到 "()()" + "()" + "()(())" = "()()()()(())"
48+
```
49+
50+
## 解题思路
51+
52+
### 思路 1:计数遍历
53+
54+
题目要求我们对 $s$ 进行原语化分解,并且删除分解中每个原语字符串的最外层括号。
55+
56+
通过观察可以发现,每个原语其实就是一组有效的括号对(左右括号匹配时),此时我们需要删除这组有效括号对的最外层括号。
57+
58+
我们可以使用一个计数器 $cnt$ 来进行原语化分解,并删除每个原语的最外层括号。
59+
60+
当计数器遇到左括号时,令计数器 $cnt$ 加 $1$,当计数器遇到右括号时,令计数器 $cnt$ 减 $1$。这样当计数器为 $0$ 时表示当前左右括号匹配。
61+
62+
为了删除每个原语的最外层括号,当遇到每个原语最外侧的左括号时(此时 $cnt$ 必然等于 $0$,因为之前字符串为空或者为上一个原语字符串),因为我们不需要最外层的左括号,所以此时我们不需要将其存入答案字符串中。只有当 $cnt > 0$ 时,才将其存入答案字符串中。
63+
64+
同理,当遇到每个原语最外侧的右括号时(此时 $cnt$ 必然等于 $1$,因为之前字符串差一个右括号匹配),因为我们不需要最外层的右括号,所以此时我们不需要将其存入答案字符串中。只有当 $cnt > 1$ 时,才将其存入答案字符串中。
65+
66+
具体步骤如下:
67+
68+
1. 遍历字符串 $s$。
69+
2. 如果遇到 `'('`,判断当前计数器是否大于 $0$:
70+
1. 如果 $cnt > 0$,则将 `'('` 存入答案字符串中,并令计数器加 $1$,即:`cnt += 1`
71+
2. 如果 $cnt == 0$,则令计数器加 $1$,即:`cnt += 1`
72+
3. 如果遇到 `')'`,判断当前计数器是否大于 $1$:
73+
1. 如果 $cnt > 1$,则将 `')'` 存入答案字符串中,并令计数器减 $1$,即:`cnt -= 1`
74+
2. 如果 $cnt == 1$,则令计数器减 $1$,即:`cnt -= 1`
75+
4. 遍历完返回答案字符串 $ans$。
76+
77+
### 思路 1:代码
78+
79+
```Python
80+
class Solution:
81+
def removeOuterParentheses(self, s: str) -> str:
82+
cnt, ans = 0, ""
83+
84+
for ch in s:
85+
if ch == '(':
86+
if cnt > 0:
87+
ans += ch
88+
cnt += 1
89+
else:
90+
if cnt > 1:
91+
ans += ch
92+
cnt -= 1
93+
94+
return ans
95+
```
96+
97+
### 思路 1:复杂度分析
98+
99+
- **时间复杂度**:$O(n)$,其中 $n$ 为字符串 $s$ 的长度。
100+
- **空间复杂度**:$O(n)$。
101+
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# [1827. 最少操作使数组递增](https://leetcode.cn/problems/minimum-operations-to-make-the-array-increasing/)
2+
3+
- 标签:贪心、数组
4+
- 难度:简单
5+
6+
## 题目链接
7+
8+
- [1827. 最少操作使数组递增 - 力扣](https://leetcode.cn/problems/minimum-operations-to-make-the-array-increasing/)
9+
10+
## 题目大意
11+
12+
**描述**:给定一个整数数组 $nums$(下标从 $0$ 开始)。每一次操作中,你可以选择数组中的一个元素,并将它增加 $1$。
13+
14+
- 比方说,如果 $nums = [1,2,3]$,你可以选择增加 $nums[1]$ 得到 $nums = [1,3,3]$。
15+
16+
**要求**:请你返回使 $nums$ 严格递增的最少操作次数。
17+
18+
**说明**
19+
20+
- 我们称数组 $nums$ 是严格递增的,当它满足对于所有的 $0 \le i < nums.length - 1$ 都有 $nums[i] < nums[i + 1]$。一个长度为 $1$ 的数组是严格递增的一种特殊情况。
21+
- $1 \le nums.length \le 5000$。
22+
- $1 \le nums[i] \le 10^4$。
23+
24+
**示例**
25+
26+
- 示例 1:
27+
28+
```python
29+
输入:nums = [1,1,1]
30+
输出:3
31+
解释:你可以进行如下操作:
32+
1) 增加 nums[2] ,数组变为 [1,1,2]。
33+
2) 增加 nums[1] ,数组变为 [1,2,2]。
34+
3) 增加 nums[2] ,数组变为 [1,2,3]。
35+
```
36+
37+
- 示例 2:
38+
39+
```python
40+
输入:nums = [1,5,2,4,1]
41+
输出:14
42+
```
43+
44+
## 解题思路
45+
46+
### 思路 1:贪心算法
47+
48+
题目要求使 $nums$ 严格递增的最少操作次数。当遇到 $nums[i - 1] \ge nums[i]$ 时,我们应该在满足要求的同时,尽可能使得操作次数最少,则 $nums[i]$ 应增加到 $nums[i - 1] + 1$ 时,此时操作次数最少,并且满足 $nums[i - 1] < nums[i]$。
49+
50+
具体操作步骤如下:
51+
52+
1. 从左到右依次遍历数组元素。
53+
2. 如果遇到 $nums[i - 1] \ge nums[i]$ 时:
54+
1. 本次增加的最少操作次数为 $nums[i - 1] + 1 - nums[i]$,将其计入答案中。
55+
2. 将 $nums[i]$ 变为 $nums[i - 1] + 1$。
56+
3. 遍历完返回答案 $ans$。
57+
58+
### 思路 1:代码
59+
60+
```Python
61+
class Solution:
62+
def minOperations(self, nums: List[int]) -> int:
63+
ans = 0
64+
for i in range(1, len(nums)):
65+
if nums[i - 1] >= nums[i]:
66+
ans += nums[i - 1] + 1 - nums[i]
67+
nums[i] = nums[i - 1] + 1
68+
69+
return ans
70+
```
71+
72+
### 思路 1:复杂度分析
73+
74+
- **时间复杂度**:$O(n)$,其中 $n$ 为数组 $nums$ 的长度。
75+
- **空间复杂度**:$O(1)$。

0 commit comments

Comments
 (0)