Skip to content

Commit 68a23b7

Browse files
authored
Merge branch 'itcharge:main' into main
2 parents 5699c38 + 5a34693 commit 68a23b7

File tree

5 files changed

+268
-2
lines changed

5 files changed

+268
-2
lines changed

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

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# LeetCode 题解(已完成 844 道)
1+
# LeetCode 题解(已完成 847 道)
22

33
| 题号 | 标题 | 题解 | 标签 | 难度 |
44
| :------ | :------ | :------ | :------ | :------ |
@@ -461,6 +461,7 @@
461461
| 0887 | [鸡蛋掉落](https://leetcode.cn/problems/super-egg-drop/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0887.%20%E9%B8%A1%E8%9B%8B%E6%8E%89%E8%90%BD.md) | 数学、二分查找、动态规划 | 困难 |
462462
| 0889 | [根据前序和后序遍历构造二叉树](https://leetcode.cn/problems/construct-binary-tree-from-preorder-and-postorder-traversal/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0889.%20%E6%A0%B9%E6%8D%AE%E5%89%8D%E5%BA%8F%E5%92%8C%E5%90%8E%E5%BA%8F%E9%81%8D%E5%8E%86%E6%9E%84%E9%80%A0%E4%BA%8C%E5%8F%89%E6%A0%91.md) | 树、数组、哈希表、分治、二叉树 | 中等 |
463463
| 0897 | [递增顺序搜索树](https://leetcode.cn/problems/increasing-order-search-tree/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0897.%20%E9%80%92%E5%A2%9E%E9%A1%BA%E5%BA%8F%E6%90%9C%E7%B4%A2%E6%A0%91.md) | 栈、树、深度优先搜索、二叉搜索树、二叉树 | 简单 |
464+
| 0900 | [RLE 迭代器](https://leetcode.cn/problems/rle-iterator/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0900.%20RLE%20%E8%BF%AD%E4%BB%A3%E5%99%A8.md) | 设计、数组、计数、迭代器 | 中等 |
464465
| 0901 | [股票价格跨度](https://leetcode.cn/problems/online-stock-span/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0901.%20%E8%82%A1%E7%A5%A8%E4%BB%B7%E6%A0%BC%E8%B7%A8%E5%BA%A6.md) | 栈、设计、数据流、单调栈 | 中等 |
465466
| 0902 | [最大为 N 的数字组合](https://leetcode.cn/problems/numbers-at-most-n-given-digit-set/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0902.%20%E6%9C%80%E5%A4%A7%E4%B8%BA%20N%20%E7%9A%84%E6%95%B0%E5%AD%97%E7%BB%84%E5%90%88.md) | 数组、数学、字符串、二分查找、动态规划 | 困难 |
466467
| 0904 | [水果成篮](https://leetcode.cn/problems/fruit-into-baskets/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0904.%20%E6%B0%B4%E6%9E%9C%E6%88%90%E7%AF%AE.md) | 数组、哈希表、滑动窗口 | 中等 |
@@ -562,6 +563,7 @@
562563
| 1347 | [制造字母异位词的最小步骤数](https://leetcode.cn/problems/minimum-number-of-steps-to-make-two-strings-anagram/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1347.%20%E5%88%B6%E9%80%A0%E5%AD%97%E6%AF%8D%E5%BC%82%E4%BD%8D%E8%AF%8D%E7%9A%84%E6%9C%80%E5%B0%8F%E6%AD%A5%E9%AA%A4%E6%95%B0.md) | 哈希表、字符串、计数 | 中等 |
563564
| 1349 | [参加考试的最大学生数](https://leetcode.cn/problems/maximum-students-taking-exam/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1349.%20%E5%8F%82%E5%8A%A0%E8%80%83%E8%AF%95%E7%9A%84%E6%9C%80%E5%A4%A7%E5%AD%A6%E7%94%9F%E6%95%B0.md) | 位运算、数组、动态规划、状态压缩、矩阵 | 困难 |
564565
| 1358 | [包含所有三种字符的子字符串数目](https://leetcode.cn/problems/number-of-substrings-containing-all-three-characters/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1358.%20%E5%8C%85%E5%90%AB%E6%89%80%E6%9C%89%E4%B8%89%E7%A7%8D%E5%AD%97%E7%AC%A6%E7%9A%84%E5%AD%90%E5%AD%97%E7%AC%A6%E4%B8%B2%E6%95%B0%E7%9B%AE.md) | 哈希表、字符串、滑动窗口 | 中等 |
566+
| 1362 | [最接近的因数](https://leetcode.cn/problems/closest-divisors/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1362.%20%E6%9C%80%E6%8E%A5%E8%BF%91%E7%9A%84%E5%9B%A0%E6%95%B0.md) | 数学 | 中等 |
565567
| 1381 | [设计一个支持增量操作的栈](https://leetcode.cn/problems/design-a-stack-with-increment-operation/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1381.%20%E8%AE%BE%E8%AE%A1%E4%B8%80%E4%B8%AA%E6%94%AF%E6%8C%81%E5%A2%9E%E9%87%8F%E6%93%8D%E4%BD%9C%E7%9A%84%E6%A0%88.md) | 栈、设计、数组 | 中等 |
566568
| 1400 | [构造 K 个回文字符串](https://leetcode.cn/problems/construct-k-palindrome-strings/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1400.%20%E6%9E%84%E9%80%A0%20K%20%E4%B8%AA%E5%9B%9E%E6%96%87%E5%AD%97%E7%AC%A6%E4%B8%B2.md) | 贪心、哈希表、字符串、计数 | 中等 |
567569
| 1408 | [数组中的字符串匹配](https://leetcode.cn/problems/string-matching-in-an-array/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1408.%20%E6%95%B0%E7%BB%84%E4%B8%AD%E7%9A%84%E5%AD%97%E7%AC%A6%E4%B8%B2%E5%8C%B9%E9%85%8D.md) | 数组、字符串、字符串匹配 | 简单 |
@@ -625,6 +627,7 @@
625627
| 1903 | [字符串中的最大奇数](https://leetcode.cn/problems/largest-odd-number-in-string/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1903.%20%E5%AD%97%E7%AC%A6%E4%B8%B2%E4%B8%AD%E7%9A%84%E6%9C%80%E5%A4%A7%E5%A5%87%E6%95%B0.md) | 贪心、数学、字符串 | 简单 |
626628
| 1925 | [统计平方和三元组的数目](https://leetcode.cn/problems/count-square-sum-triples/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1925.%20%E7%BB%9F%E8%AE%A1%E5%B9%B3%E6%96%B9%E5%92%8C%E4%B8%89%E5%85%83%E7%BB%84%E7%9A%84%E6%95%B0%E7%9B%AE.md) | 数学、枚举 | 简单 |
627629
| 1929 | [数组串联](https://leetcode.cn/problems/concatenation-of-array/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1929.%20%E6%95%B0%E7%BB%84%E4%B8%B2%E8%81%94.md) | 数组 | 简单 |
630+
| 1930 | [长度为 3 的不同回文子序列](https://leetcode.cn/problems/unique-length-3-palindromic-subsequences/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1930.%20%E9%95%BF%E5%BA%A6%E4%B8%BA%203%20%E7%9A%84%E4%B8%8D%E5%90%8C%E5%9B%9E%E6%96%87%E5%AD%90%E5%BA%8F%E5%88%97.md) | 哈希表、字符串、前缀和 | 中等 |
628631
| 1936 | [新增的最少台阶数](https://leetcode.cn/problems/add-minimum-number-of-rungs/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1936.%20%E6%96%B0%E5%A2%9E%E7%9A%84%E6%9C%80%E5%B0%91%E5%8F%B0%E9%98%B6%E6%95%B0.md) | 贪心、数组 | 中等 |
629632
| 1941 | [检查是否所有字符出现次数相同](https://leetcode.cn/problems/check-if-all-characters-have-equal-number-of-occurrences/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1941.%20%E6%A3%80%E6%9F%A5%E6%98%AF%E5%90%A6%E6%89%80%E6%9C%89%E5%AD%97%E7%AC%A6%E5%87%BA%E7%8E%B0%E6%AC%A1%E6%95%B0%E7%9B%B8%E5%90%8C.md) | 哈希表、字符串、计数 | 简单 |
630633
| 1947 | [最大兼容性评分和](https://leetcode.cn/problems/maximum-compatibility-score-sum/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1947.%20%E6%9C%80%E5%A4%A7%E5%85%BC%E5%AE%B9%E6%80%A7%E8%AF%84%E5%88%86%E5%92%8C.md) | 位运算、数组、动态规划、回溯、状态压缩 | 中等 |

README.md

+1-1
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 题解(已完成 844 道)](./Contents/00.Introduction/04.Solutions-List.md)
258+
## [12. LeetCode 题解(已完成 847 道)](./Contents/00.Introduction/04.Solutions-List.md)

Solutions/0900. RLE 迭代器.md

+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
# [0900. RLE 迭代器](https://leetcode.cn/problems/rle-iterator/)
2+
3+
- 标签:设计、数组、计数、迭代器
4+
- 难度:中等
5+
6+
## 题目链接
7+
8+
- [0900. RLE 迭代器 - 力扣](https://leetcode.cn/problems/rle-iterator/)
9+
10+
## 题目大意
11+
12+
**描述**:我们可以使用游程编码(即 RLE)来编码一个整数序列。在偶数长度 $encoding$ ( 从 $0$ 开始 )的游程编码数组中,对于所有偶数 $i$,$encoding[i]$ 告诉我们非负整数 $encoding[i + 1]$ 在序列中重复的次数。
13+
14+
- 例如,序列 $arr = [8,8,8,5,5]$ 可以被编码为 $encoding =[3,8,2,5]$。$encoding =[3,8,0,9,2,5]$ 和 $encoding =[2,8,1,8,2,5]$ 也是 $arr$ 有效的 RLE。
15+
16+
给定一个游程长度的编码数组 $encoding$。
17+
18+
**要求**:设计一个迭代器来遍历它。
19+
20+
实现 `RLEIterator` 类:
21+
22+
- `RLEIterator(int[] encoded)` 用编码后的数组初始化对象。
23+
- `int next(int n)` 以这种方式耗尽后 $n$ 个元素并返回最后一个耗尽的元素。如果没有剩余的元素要耗尽,则返回 $-1$。
24+
25+
**说明**
26+
27+
- $2 \le encoding.length \le 1000$。
28+
- $encoding.length$ 为偶。
29+
- $0 \le encoding[i] \le 10^9$。
30+
- $1 \le n \le 10^9$。
31+
- 每个测试用例调用 `next` 不高于 $1000$ 次。
32+
33+
**示例**
34+
35+
- 示例 1:
36+
37+
```python
38+
输入:
39+
["RLEIterator","next","next","next","next"]
40+
[[[3,8,0,9,2,5]],[2],[1],[1],[2]]
41+
输出:
42+
[null,8,8,5,-1]
43+
解释:
44+
RLEIterator rLEIterator = new RLEIterator([3, 8, 0, 9, 2, 5]); // 这映射到序列 [8,8,8,5,5]。
45+
rLEIterator.next(2); // 耗去序列的 2 个项,返回 8。现在剩下的序列是 [8, 5, 5]。
46+
rLEIterator.next(1); // 耗去序列的 1 个项,返回 8。现在剩下的序列是 [5, 5]。
47+
rLEIterator.next(1); // 耗去序列的 1 个项,返回 5。现在剩下的序列是 [5]。
48+
rLEIterator.next(2); // 耗去序列的 2 个项,返回 -1。 这是由于第一个被耗去的项是 5
49+
但第二个项并不存在。由于最后一个要耗去的项不存在,我们返回 -1
50+
```
51+
52+
## 解题思路
53+
54+
### 思路 1:模拟
55+
56+
1. 初始化时:
57+
1. 保存数组 $encoding$ 作为成员变量。
58+
2. 保存当前位置 $index$,表示当前迭代器指向元素 $encoding[index + 1]$。初始化赋值为 $0$。
59+
3. 保存当前指向元素 $encoding[index + 1]$ 已经被删除的元素个数 $d\underline{}cnt$。初始化赋值为 $0$。
60+
2. 调用 `next(n)` 时:
61+
1. 对于当前元素,先判断当前位置是否超出 $encoding$ 范围,超过则直接返回 $-1$。
62+
2. 如果未超过,再判断当前元素剩余个数 $encoding[index] - d\underline{}cnt$ 是否小于 $n$ 个。
63+
1. 如果小于 $n$ 个,则删除当前元素剩余所有个数,并指向下一位置继续删除剩余元素。
64+
2. 如果等于大于等于 $n$ 个,则令当前指向元素 $encoding[index + 1]$ 已经被删除的元素个数 $d\underline{}cnt$ 加上 $n$。
65+
66+
### 思路 1:代码
67+
68+
```Python
69+
class RLEIterator:
70+
71+
def __init__(self, encoding: List[int]):
72+
self.encoding = encoding
73+
self.index = 0
74+
self.d_cnt = 0
75+
76+
def next(self, n: int) -> int:
77+
while self.index < len(self.encoding):
78+
if self.d_cnt + n > self.encoding[self.index]:
79+
n -= self.encoding[self.index] - self.d_cnt
80+
self.d_cnt = 0
81+
self.index += 2
82+
else:
83+
self.d_cnt += n
84+
return self.encoding[self.index + 1]
85+
return -1
86+
```
87+
88+
### 思路 1:复杂度分析
89+
90+
- **时间复杂度**:$O(n + m)$,其中 $n$ 为数组 $encoding$ 的长度,$m$ 是调用 `next(n)` 的次数。
91+
- **空间复杂度**:$O(n)$。
92+

Solutions/1362. 最接近的因数.md

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# [1362. 最接近的因数](https://leetcode.cn/problems/closest-divisors/)
2+
3+
- 标签:数学
4+
- 难度:中等
5+
6+
## 题目链接
7+
8+
- [1362. 最接近的因数 - 力扣](https://leetcode.cn/problems/closest-divisors/)
9+
10+
## 题目大意
11+
12+
**描述**:给定一个整数 $num$。
13+
14+
**要求**:找出同时满足下面全部要求的两个整数:
15+
16+
- 两数乘积等于 $num + 1$ 或 $num + 2$。
17+
- 以绝对差进行度量,两数大小最接近。
18+
19+
你可以按照任意顺序返回这两个整数。
20+
21+
**说明**
22+
23+
- $1 \le num \le 10^9$。
24+
25+
**示例**
26+
27+
- 示例 1:
28+
29+
```python
30+
输入:num = 8
31+
输出:[3,3]
32+
解释:对于 num + 1 = 9,最接近的两个因数是 3 & 3;对于 num + 2 = 10, 最接近的两个因数是 2 & 5,因此返回 3 & 3
33+
```
34+
35+
- 示例 2:
36+
37+
```python
38+
输入:num = 123
39+
输出:[5,25]
40+
```
41+
42+
## 解题思路
43+
44+
### 思路 1:数学
45+
46+
对于整数的任意一个范围在 $[\sqrt{n}, n]$ 的因数而言,一定存在一个范围在 $[1, \sqrt{n}]$ 的因数与其对应。因此,我们在遍历整数因数时,我们只需遍历 $[1, \sqrt{n}]$ 范围内的因数即可。
47+
48+
则这道题的具体解题步骤如下:
49+
50+
1. 对于整数 $num + 1$、从 $\sqrt{num + 1}$ 的位置开始,到 $1$ 为止,以递减的顺序在 $[1, \sqrt{num + 1}]$ 范围内找到最接近的小因数 $a1$,并根据 $num // a1$ 获得另一个因数 $a2$。
51+
2. 用同样的方式,对于整数 $num + 2$、从 $\sqrt{num + 2}$ 的位置开始,到 $1$ 为止,以递减的顺序在 $[1, \sqrt{num + 2}]$ 范围内找到最接近的小因数 $b1$,并根据 $num // b1$ 获得另一个因数 $b2$。
52+
3. 判断 $abs(a1 - a2)$ 与 $abs(b1 - b2)$ 的大小,返回差值绝对值较小的一对因子数作为答案。
53+
54+
### 思路 1:代码
55+
56+
```Python
57+
class Solution:
58+
def disassemble(self, num):
59+
for i in range(int(sqrt(num) + 1), 1, -1):
60+
if num % i == 0:
61+
return (i, num // i)
62+
return (1, num)
63+
64+
def closestDivisors(self, num: int) -> List[int]:
65+
a1, a2 = self.disassemble(num + 1)
66+
b1, b2 = self.disassemble(num + 2)
67+
if abs(a1 - a2) <= abs(b1 - b2):
68+
return [a1, a2]
69+
return [b1, b2]
70+
```
71+
72+
### 思路 1:复杂度分析
73+
74+
- **时间复杂度**:$(\sqrt{n})$。
75+
- **空间复杂度**:$O(1)$。
76+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
# [1930. 长度为 3 的不同回文子序列](https://leetcode.cn/problems/unique-length-3-palindromic-subsequences/)
2+
3+
- 标签:哈希表、字符串、前缀和
4+
- 难度:中等
5+
6+
## 题目链接
7+
8+
- [1930. 长度为 3 的不同回文子序列 - 力扣](https://leetcode.cn/problems/unique-length-3-palindromic-subsequences/)
9+
10+
## 题目大意
11+
12+
**描述**:给定一个人字符串 $s$。
13+
14+
**要求**:返回 $s$ 中长度为 $s$ 的不同回文子序列的个数。即便存在多种方法来构建相同的子序列,但相同的子序列只计数一次。
15+
16+
**说明**
17+
18+
- **回文**:指正着读和反着读一样的字符串。
19+
- **子序列**:由原字符串删除其中部分字符(也可以不删除)且不改变剩余字符之间相对顺序形成的一个新字符串。
20+
- 例如,`"ace"``"abcde"` 的一个子序列。
21+
22+
- $3 \le s.length \le 10^5$。
23+
- $s$ 仅由小写英文字母组成。
24+
25+
**示例**
26+
27+
- 示例 1:
28+
29+
```python
30+
输入:s = "aabca"
31+
输出:3
32+
解释:长度为 33 个回文子序列分别是:
33+
- "aba" ("aabca" 的子序列)
34+
- "aaa" ("aabca" 的子序列)
35+
- "aca" ("aabca" 的子序列)
36+
```
37+
38+
- 示例 2:
39+
40+
```python
41+
输入:s = "bbcbaba"
42+
输出:4
43+
解释:长度为 34 个回文子序列分别是:
44+
- "bbb" ("bbcbaba" 的子序列)
45+
- "bcb" ("bbcbaba" 的子序列)
46+
- "bab" ("bbcbaba" 的子序列)
47+
- "aba" ("bbcbaba" 的子序列)
48+
```
49+
50+
## 解题思路
51+
52+
### 思路 1:枚举 + 哈希表
53+
54+
字符集只包含 $26$ 个小写字母,所以我们可以枚举这 $26$ 个小写字母。
55+
56+
对于每个小写字母,使用对撞双指针,找到字符串 $s$ 首尾两侧与小写字母相同的最左位置和最右位置。
57+
58+
如果两个位置不同,则我们可以将两个位置中间不重复的字符当作是长度为 $3$ 的子序列最中间的那个字符。
59+
60+
则我们可以统计出两个位置中间不重复字符的个数,将其累加到答案中。
61+
62+
遍历完,返回答案。
63+
64+
### 思路 1:代码
65+
66+
```Python
67+
class Solution:
68+
def countPalindromicSubsequence(self, s: str) -> int:
69+
size = len(s)
70+
ans = 0
71+
72+
for i in range(26):
73+
left, right = 0, size - 1
74+
75+
while left < size and ord(s[left]) - ord('a') != i:
76+
left += 1
77+
78+
while right >= 0 and ord(s[right]) - ord('a') != i:
79+
right -= 1
80+
81+
if right - left < 2:
82+
continue
83+
84+
char_set = set()
85+
for j in range(left + 1, right):
86+
char_set.add(s[j])
87+
ans += len(char_set)
88+
89+
return ans
90+
```
91+
92+
### 思路 1:复杂度分析
93+
94+
- **时间复杂度**:$n \times | \sum | + | \sum |^2$,其中 $n$ 为字符串 $s$ 的长度,$\sum$ 为字符集,本题中 $| \sum | = 26$。
95+
- **空间复杂度**:$O(| \sum |)$。

0 commit comments

Comments
 (0)