Skip to content

Commit

Permalink
更新题解列表
Browse files Browse the repository at this point in the history
  • Loading branch information
itcharge committed Jan 8, 2024
1 parent f819b24 commit 0209deb
Show file tree
Hide file tree
Showing 5 changed files with 220 additions and 13 deletions.
4 changes: 3 additions & 1 deletion Contents/00.Introduction/04.Solutions-List.md
@@ -1,4 +1,4 @@
# LeetCode 题解(已完成 832 道)
# LeetCode 题解(已完成 834 道)

| 题号 | 标题 | 题解 | 标签 | 难度 |
| :------ | :------ | :------ | :------ | :------ |
Expand Down Expand Up @@ -499,6 +499,7 @@
| 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) | 数学、动态规划 | 困难 |
| 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) | 数组、动态规划 | 中等 |
| 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) | 深度优先搜索、广度优先搜索、并查集、数组、矩阵 | 中等 |
| 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) | 栈、字符串 | 简单 |
| 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) | 字典树、双指针、字符串、字符串匹配 | 中等 |
| 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) | 脑筋急转弯、数学、动态规划、博弈 | 简单 |
| 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) | 树、深度优先搜索、字符串、二叉树 | 困难 |
Expand Down Expand Up @@ -602,6 +603,7 @@
| 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) | 哈希表、字符串、计数 | 简单 |
| 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) || 简单 |
| 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) | 数组、数学 | 简单 |
| 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) | 贪心、数组 | 简单 |
| 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) | 贪心、数组、排序 | 中等 |
| 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) | 字符串 | 简单 |
| 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) | 深度优先搜索、字典树 | 中等 |
Expand Down
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -255,4 +255,4 @@
- [动态规划优化题目](./Contents/10.Dynamic-Programming/11.DP-Optimization/04.DP-Optimization-List.md)

## 11. 附加内容
## [12. LeetCode 题解(已完成 832 道)](./Contents/00.Introduction/04.Solutions-List.md)
## [12. LeetCode 题解(已完成 834 道)](./Contents/00.Introduction/04.Solutions-List.md)
51 changes: 40 additions & 11 deletions Solutions/0487. 最大连续1的个数 II.md
Expand Up @@ -9,25 +9,49 @@

## 题目大意

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

要求:找出其中最大连续 `1` 的个数。
**要求**:如果最多可以翻转一个 $0$,则返回数组中连续 $1$ 的最大个数。

**说明**

- 1 <= nums.length <= 105
nums[i] 不是 0 就是 1.

**示例**

- 示例 1:

```python
输入:nums = [1,0,1,1,0]
输出:4
解释:翻转第一个 0 可以得到最长的连续 1。当翻转以后,最大连续 1 的个数为 4
```

- 示例 2:

```python
输入:nums = [1,0,1,1,0,1]
输出:4
```

## 解题思路

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

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

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

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

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

### 思路 1:代码

```python
class Solution:
Expand All @@ -49,3 +73,8 @@ class Solution:
return ans
```

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

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

101 changes: 101 additions & 0 deletions Solutions/1021. 删除最外层的括号.md
@@ -0,0 +1,101 @@
# [1021. 删除最外层的括号](https://leetcode.cn/problems/remove-outermost-parentheses/)

- 标签:栈、字符串
- 难度:简单

## 题目链接

- [1021. 删除最外层的括号 - 力扣](https://leetcode.cn/problems/remove-outermost-parentheses/)

## 题目大意

**描述**:有效括号字符串为空 `""``"("` + $A$ + `")"` 或 $A + B$ ,其中 $A$ 和 $B$ 都是有效的括号字符串,$+$ 代表字符串的连接。

- 例如,`""``"()"``"(())()"``"(()(()))"` 都是有效的括号字符串。

如果有效字符串 $s$ 非空,且不存在将其拆分为 $s = A + B$ 的方法,我们称其为原语(primitive),其中 $A$ 和 $B$ 都是非空有效括号字符串。

给定一个非空有效字符串 $s$,考虑将其进行原语化分解,使得:$s = P_1 + P_2 + ... + P_k$,其中 $P_i$ 是有效括号字符串原语。

**要求**:对 $s$ 进行原语化分解,删除分解中每个原语字符串的最外层括号,返回 $s$。

**说明**

- $1 \le s.length \le 10^5$。
- $s[i]$ 为 `'('``')'`
- $s$ 是一个有效括号字符串。

**示例**

- 示例 1:

```python
输入:s = "(()())(())"
输出:"()()()"
解释:
输入字符串为 "(()())(())",原语化分解得到 "(()())" + "(())"
删除每个部分中的最外层括号后得到 "()()" + "()" = "()()()"
```

- 示例 2:

```python
输入:s = "(()())(())(()(()))"
输出:"()()()()(())"
解释:
输入字符串为 "(()())(())(()(()))",原语化分解得到 "(()())" + "(())" + "(()(()))"
删除每个部分中的最外层括号后得到 "()()" + "()" + "()(())" = "()()()()(())"
```

## 解题思路

### 思路 1:计数遍历

题目要求我们对 $s$ 进行原语化分解,并且删除分解中每个原语字符串的最外层括号。

通过观察可以发现,每个原语其实就是一组有效的括号对(左右括号匹配时),此时我们需要删除这组有效括号对的最外层括号。

我们可以使用一个计数器 $cnt$ 来进行原语化分解,并删除每个原语的最外层括号。

当计数器遇到左括号时,令计数器 $cnt$ 加 $1$,当计数器遇到右括号时,令计数器 $cnt$ 减 $1$。这样当计数器为 $0$ 时表示当前左右括号匹配。

为了删除每个原语的最外层括号,当遇到每个原语最外侧的左括号时(此时 $cnt$ 必然等于 $0$,因为之前字符串为空或者为上一个原语字符串),因为我们不需要最外层的左括号,所以此时我们不需要将其存入答案字符串中。只有当 $cnt > 0$ 时,才将其存入答案字符串中。

同理,当遇到每个原语最外侧的右括号时(此时 $cnt$ 必然等于 $1$,因为之前字符串差一个右括号匹配),因为我们不需要最外层的右括号,所以此时我们不需要将其存入答案字符串中。只有当 $cnt > 1$ 时,才将其存入答案字符串中。

具体步骤如下:

1. 遍历字符串 $s$。
2. 如果遇到 `'('`,判断当前计数器是否大于 $0$:
1. 如果 $cnt > 0$,则将 `'('` 存入答案字符串中,并令计数器加 $1$,即:`cnt += 1`
2. 如果 $cnt == 0$,则令计数器加 $1$,即:`cnt += 1`
3. 如果遇到 `')'`,判断当前计数器是否大于 $1$:
1. 如果 $cnt > 1$,则将 `')'` 存入答案字符串中,并令计数器减 $1$,即:`cnt -= 1`
2. 如果 $cnt == 1$,则令计数器减 $1$,即:`cnt -= 1`
4. 遍历完返回答案字符串 $ans$。

### 思路 1:代码

```Python
class Solution:
def removeOuterParentheses(self, s: str) -> str:
cnt, ans = 0, ""

for ch in s:
if ch == '(':
if cnt > 0:
ans += ch
cnt += 1
else:
if cnt > 1:
ans += ch
cnt -= 1

return ans
```

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

- **时间复杂度**:$O(n)$,其中 $n$ 为字符串 $s$ 的长度。
- **空间复杂度**:$O(n)$。

75 changes: 75 additions & 0 deletions Solutions/1827. 最少操作使数组递增.md
@@ -0,0 +1,75 @@
# [1827. 最少操作使数组递增](https://leetcode.cn/problems/minimum-operations-to-make-the-array-increasing/)

- 标签:贪心、数组
- 难度:简单

## 题目链接

- [1827. 最少操作使数组递增 - 力扣](https://leetcode.cn/problems/minimum-operations-to-make-the-array-increasing/)

## 题目大意

**描述**:给定一个整数数组 $nums$(下标从 $0$ 开始)。每一次操作中,你可以选择数组中的一个元素,并将它增加 $1$。

- 比方说,如果 $nums = [1,2,3]$,你可以选择增加 $nums[1]$ 得到 $nums = [1,3,3]$。

**要求**:请你返回使 $nums$ 严格递增的最少操作次数。

**说明**

- 我们称数组 $nums$ 是严格递增的,当它满足对于所有的 $0 \le i < nums.length - 1$ 都有 $nums[i] < nums[i + 1]$。一个长度为 $1$ 的数组是严格递增的一种特殊情况。
- $1 \le nums.length \le 5000$。
- $1 \le nums[i] \le 10^4$。

**示例**

- 示例 1:

```python
输入:nums = [1,1,1]
输出:3
解释:你可以进行如下操作:
1) 增加 nums[2] ,数组变为 [1,1,2]。
2) 增加 nums[1] ,数组变为 [1,2,2]。
3) 增加 nums[2] ,数组变为 [1,2,3]。
```

- 示例 2:

```python
输入:nums = [1,5,2,4,1]
输出:14
```

## 解题思路

### 思路 1:贪心算法

题目要求使 $nums$ 严格递增的最少操作次数。当遇到 $nums[i - 1] \ge nums[i]$ 时,我们应该在满足要求的同时,尽可能使得操作次数最少,则 $nums[i]$ 应增加到 $nums[i - 1] + 1$ 时,此时操作次数最少,并且满足 $nums[i - 1] < nums[i]$。

具体操作步骤如下:

1. 从左到右依次遍历数组元素。
2. 如果遇到 $nums[i - 1] \ge nums[i]$ 时:
1. 本次增加的最少操作次数为 $nums[i - 1] + 1 - nums[i]$,将其计入答案中。
2. 将 $nums[i]$ 变为 $nums[i - 1] + 1$。
3. 遍历完返回答案 $ans$。

### 思路 1:代码

```Python
class Solution:
def minOperations(self, nums: List[int]) -> int:
ans = 0
for i in range(1, len(nums)):
if nums[i - 1] >= nums[i]:
ans += nums[i - 1] + 1 - nums[i]
nums[i] = nums[i - 1] + 1

return ans
```

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

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

0 comments on commit 0209deb

Please sign in to comment.