Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit fd1e206

Browse files
committed
更新题解列表
1 parent 02fec6c commit fd1e206

7 files changed

+363
-23
lines changed

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

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

33
| 题号 | 标题 | 题解 | 标签 | 难度 |
44
| :------ | :------ | :------ | :------ | :------ |
@@ -555,9 +555,11 @@
555555
| 1313 | [解压缩编码列表](https://leetcode.cn/problems/decompress-run-length-encoded-list/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1313.%20%E8%A7%A3%E5%8E%8B%E7%BC%A9%E7%BC%96%E7%A0%81%E5%88%97%E8%A1%A8.md) | 数组 | 简单 |
556556
| 1317 | [将整数转换为两个无零整数的和](https://leetcode.cn/problems/convert-integer-to-the-sum-of-two-no-zero-integers/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1317.%20%E5%B0%86%E6%95%B4%E6%95%B0%E8%BD%AC%E6%8D%A2%E4%B8%BA%E4%B8%A4%E4%B8%AA%E6%97%A0%E9%9B%B6%E6%95%B4%E6%95%B0%E7%9A%84%E5%92%8C.md) | 数学 | 简单 |
557557
| 1319 | [连通网络的操作次数](https://leetcode.cn/problems/number-of-operations-to-make-network-connected/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1319.%20%E8%BF%9E%E9%80%9A%E7%BD%91%E7%BB%9C%E7%9A%84%E6%93%8D%E4%BD%9C%E6%AC%A1%E6%95%B0.md) | 深度优先搜索、广度优先搜索、并查集、图 | 中等 |
558+
| 1324 | [竖直打印单词](https://leetcode.cn/problems/print-words-vertically/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1324.%20%E7%AB%96%E7%9B%B4%E6%89%93%E5%8D%B0%E5%8D%95%E8%AF%8D.md) | 数组、字符串、模拟 | 中等 |
558559
| 1338 | [数组大小减半](https://leetcode.cn/problems/reduce-array-size-to-the-half/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1338.%20%E6%95%B0%E7%BB%84%E5%A4%A7%E5%B0%8F%E5%87%8F%E5%8D%8A.md) | 贪心、数组、哈希表、排序、堆(优先队列) | 中等 |
559560
| 1343 | [大小为 K 且平均值大于等于阈值的子数组数目](https://leetcode.cn/problems/number-of-sub-arrays-of-size-k-and-average-greater-than-or-equal-to-threshold/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1343.%20%E5%A4%A7%E5%B0%8F%E4%B8%BA%20K%20%E4%B8%94%E5%B9%B3%E5%9D%87%E5%80%BC%E5%A4%A7%E4%BA%8E%E7%AD%89%E4%BA%8E%E9%98%88%E5%80%BC%E7%9A%84%E5%AD%90%E6%95%B0%E7%BB%84%E6%95%B0%E7%9B%AE.md) | 数组、滑动窗口 | 中等 |
560561
| 1344 | [时钟指针的夹角](https://leetcode.cn/problems/angle-between-hands-of-a-clock/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1344.%20%E6%97%B6%E9%92%9F%E6%8C%87%E9%92%88%E7%9A%84%E5%A4%B9%E8%A7%92.md) | 数学 | 中等 |
562+
| 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) | 哈希表、字符串、计数 | 中等 |
561563
| 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) | 位运算、数组、动态规划、状态压缩、矩阵 | 困难 |
562564
| 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) | 哈希表、字符串、滑动窗口 | 中等 |
563565
| 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) | 栈、设计、数组 | 中等 |
@@ -572,6 +574,7 @@
572574
| 1450 | [在既定时间做作业的学生人数](https://leetcode.cn/problems/number-of-students-doing-homework-at-a-given-time/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1450.%20%E5%9C%A8%E6%97%A2%E5%AE%9A%E6%97%B6%E9%97%B4%E5%81%9A%E4%BD%9C%E4%B8%9A%E7%9A%84%E5%AD%A6%E7%94%9F%E4%BA%BA%E6%95%B0.md) | 数组 | 简单 |
573575
| 1451 | [重新排列句子中的单词](https://leetcode.cn/problems/rearrange-words-in-a-sentence/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1451.%20%E9%87%8D%E6%96%B0%E6%8E%92%E5%88%97%E5%8F%A5%E5%AD%90%E4%B8%AD%E7%9A%84%E5%8D%95%E8%AF%8D.md) | 字符串、排序 | 中等 |
574576
| 1456 | [定长子串中元音的最大数目](https://leetcode.cn/problems/maximum-number-of-vowels-in-a-substring-of-given-length/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1456.%20%E5%AE%9A%E9%95%BF%E5%AD%90%E4%B8%B2%E4%B8%AD%E5%85%83%E9%9F%B3%E7%9A%84%E6%9C%80%E5%A4%A7%E6%95%B0%E7%9B%AE.md) | 字符串、滑动窗口 | 中等 |
577+
| 1476 | [子矩形查询](https://leetcode.cn/problems/subrectangle-queries/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1476.%20%E5%AD%90%E7%9F%A9%E5%BD%A2%E6%9F%A5%E8%AF%A2.md) | 设计、数组、矩阵 | 中等 |
575578
| 1480 | [一维数组的动态和](https://leetcode.cn/problems/running-sum-of-1d-array/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1480.%20%E4%B8%80%E7%BB%B4%E6%95%B0%E7%BB%84%E7%9A%84%E5%8A%A8%E6%80%81%E5%92%8C.md) | 数组、前缀和 | 简单 |
576579
| 1482 | [制作 m 束花所需的最少天数](https://leetcode.cn/problems/minimum-number-of-days-to-make-m-bouquets/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1482.%20%E5%88%B6%E4%BD%9C%20m%20%E6%9D%9F%E8%8A%B1%E6%89%80%E9%9C%80%E7%9A%84%E6%9C%80%E5%B0%91%E5%A4%A9%E6%95%B0.md) | 数组、二分查找 | 中等 |
577580
| 1486 | [数组异或操作](https://leetcode.cn/problems/xor-operation-in-an-array/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1486.%20%E6%95%B0%E7%BB%84%E5%BC%82%E6%88%96%E6%93%8D%E4%BD%9C.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 题解(已完成 841 道)](./Contents/00.Introduction/04.Solutions-List.md)
258+
## [12. LeetCode 题解(已完成 844 道)](./Contents/00.Introduction/04.Solutions-List.md)

‎Solutions/1208. 尽可能使字符串相等.md

Lines changed: 42 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,50 @@
99

1010
## 题目大意
1111

12-
给定两个长度相同的字符串,`s``t`。将 `s` 中的第 `i` 个字符变到 `t` 中的第 `i` 个字符需要 $| s[i] - t[i] |$ 的开销(开销可能为 `0`),也就是两个字符的 ASCII 码值的差的绝对值。用于变更字符串的最大预算是 `maxCost`。在转化字符串时,总开销应当小于等于该预算,这也意味着字符串的转化可能是不完全的。
12+
**描述**:给定两个长度相同的字符串,$s$$t$。将 $s$ 中的第 $i$ 个字符变到 $t$ 中的第 $i$ 个字符需要 $| s[i] - t[i] |$ 的开销(开销可能为 0ドル$),也就是两个字符的 ASCII 码值的差的绝对值。用于变更字符串的最大预算是 $maxCost$。在转化字符串时,总开销应当小于等于该预算,这也意味着字符串的转化可能是不完全的。
1313

14-
要求:如果你可以将 `s` 的子字符串转化为它在 `t` 中对应的子字符串,则返回可以转化的最大长度。如果 `s` 中没有子字符串可以转化成 `t` 中对应的子字符串,则返回 `0`
14+
**要求**:如果你可以将 $s$ 的子字符串转化为它在 $t$ 中对应的子字符串,则返回可以转化的最大长度。如果 $s$ 中没有子字符串可以转化成 $t$ 中对应的子字符串,则返回 0ドル$。
15+
16+
**说明**:
17+
18+
- 1ドル \le s.length, t.length \le 10^5$。
19+
- 0ドル \le maxCost \le 10^6$。
20+
- $s$ 和 $t$ 都只含小写英文字母。
21+
22+
**示例**:
23+
24+
- 示例 1:
25+
26+
```python
27+
输入:s = "abcd", t = "bcdf", maxCost = 3
28+
输出:3
29+
解释:s 中的 "abc" 可以变为 "bcd"。开销为 3,所以最大长度为 3
30+
```
31+
32+
- 示例 2:
33+
34+
```python
35+
输入:s = "abcd", t = "cdef", maxCost = 3
36+
输出:1
37+
解释:s 中的任一字符要想变成 t 中对应的字符,其开销都是 2。因此,最大长度为 1
38+
```
1539

1640
## 解题思路
1741

18-
维护一个滑动窗口 `window_sum` 用于记录窗口内的开销总和,保证窗口内的开销总和小于等于 `maxCost`。使用 `ans` 记录可以转化的最大长度。具体做法如下:
42+
### 思路 1:滑动窗口
1943

20-
使用两个指针 `left``right`。分别指向滑动窗口的左右边界,保证窗口内所有元素转化开销总和小于等于 `maxCost`
44+
维护一个滑动窗口 $window\underline{}sum$ 用于记录窗口内的开销总和,保证窗口内的开销总和小于等于 $maxCost$。使用 $ans$ 记录可以转化的最大长度。具体做法如下:
2145

22-
- 先统计出 `s` 中第 `i` 个字符变为 `t` 的第 `i` 个字符的开销,用数组 `costs` 保存。
23-
- 一开始,`left``right` 都指向 `0`
24-
- 将最右侧字符的转变开销填入窗口中,向右移动 `right`
25-
- 直到窗口内开销总和 `window_sum` 大于 `maxCost`。则不断右移 `left`,缩小窗口长度。直到 `window_sum <= maxCost` 时,更新可以转换的最大长度 `ans`
26-
- 向右移动 `right`,直到 `right >= len(s)` 为止。
27-
- 输出答案 `ans`
46+
使用两个指针 $left$、$right$。分别指向滑动窗口的左右边界,保证窗口内所有元素转化开销总和小于等于 $maxCost$。
2847

29-
## 代码
48+
- 先统计出 $s$ 中第 $i$ 个字符变为 $t$ 的第 $i$ 个字符的开销,用数组 $costs$ 保存。
49+
- 一开始,$left$、$right$ 都指向 0ドル$。
50+
- 将最右侧字符的转变开销填入窗口中,向右移动 $right$。
51+
- 直到窗口内开销总和 $window\underline{}sum$ 大于 $maxCost$。则不断右移 $left,ドル缩小窗口长度。直到 $window\underline{}sum \le maxCost$ 时,更新可以转换的最大长度 $ans$。
52+
- 向右移动 $right,ドル直到 $right \ge len(s)$ 为止。
53+
- 输出答案 $ans$。
54+
55+
### 思路 1:代码
3056

3157
```python
3258
class Solution:
@@ -50,3 +76,8 @@ class Solution:
5076
return ans
5177
```
5278

79+
### 思路 1:复杂度分析
80+
81+
- **时间复杂度**:
82+
- **空间复杂度**:
83+

‎Solutions/1324. 竖直打印单词.md

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# [1324. 竖直打印单词](https://leetcode.cn/problems/print-words-vertically/)
2+
3+
- 标签:数组、字符串、模拟
4+
- 难度:中等
5+
6+
## 题目链接
7+
8+
- [1324. 竖直打印单词 - 力扣](https://leetcode.cn/problems/print-words-vertically/)
9+
10+
## 题目大意
11+
12+
**描述**:给定一个字符串 $s$。
13+
14+
**要求**:按照单词在 $s$ 中出现顺序将它们全部竖直返回。
15+
16+
**说明**:
17+
18+
- 单词应该以字符串列表的形式返回,必要时用空格补位,但输出尾部的空格需要删除(不允许尾随空格)。
19+
- 每个单词只能放在一列上,每一列中也只能有一个单词。
20+
- 1ドル \le s.length \le 200$。
21+
- $s$ 仅含大写英文字母。
22+
- 题目数据保证两个单词之间只有一个空格。
23+
24+
**示例**:
25+
26+
- 示例 1:
27+
28+
```python
29+
输入:s = "HOW ARE YOU"
30+
输出:["HAY","ORO","WEU"]
31+
解释:每个单词都应该竖直打印。
32+
"HAY"
33+
"ORO"
34+
"WEU"
35+
```
36+
37+
- 示例 2:
38+
39+
```python
40+
输入:s = "TO BE OR NOT TO BE"
41+
输出:["TBONTB","OEROOE"," T"]
42+
解释:题目允许使用空格补位,但不允许输出末尾出现空格。
43+
"TBONTB"
44+
"OEROOE"
45+
" T"
46+
```
47+
48+
## 解题思路
49+
50+
### 思路 1:模拟
51+
52+
1. 将字符串 $s$ 按空格分割为单词数组 $words$。
53+
2. 计算出单词数组 $words$ 中单词的最大长度 $max\underline{}len$。
54+
3. 第一重循环遍历竖直单词的每个单词位置 $i,ドル第二重循环遍历当前第 $j$ 个单词。
55+
1. 如果当前单词没有第 $i$ 个字符(当前单词的长度超过了单词位置 $i$),则将空格插入到竖直单词中。
56+
2. 如果当前单词有第 $i$ 个字符,泽讲当前单词的第 $i$ 个字符插入到竖直单词中。
57+
4. 第二重循环遍历完,将竖直单词去除尾随空格,并加入到答案数组中。
58+
5. 第一重循环遍历完,则返回答案数组。
59+
60+
### 思路 1:代码
61+
62+
```Python
63+
class Solution:
64+
def printVertically(self, s: str) -> List[str]:
65+
words = s.split(' ')
66+
max_len = 0
67+
for word in words:
68+
max_len = max(len(word), max_len)
69+
70+
res = []
71+
for i in range(max_len):
72+
ans = ""
73+
for j in range(len(words)):
74+
if i + 1 > len(words[j]):
75+
ans += ' '
76+
else:
77+
ans += words[j][i]
78+
res.append(ans.rstrip())
79+
80+
return res
81+
```
82+
83+
### 思路 1:复杂度分析
84+
85+
- **时间复杂度**:$O(n \times max(|word|)),ドル其中 $n$ 为字符串 $s$ 中的单词个数,$max(|word|)$ 是最长的单词长度。。
86+
- **空间复杂度**:$O(n \times max(|word|))$。
87+
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# [1347. 制造字母异位词的最小步骤数](https://leetcode.cn/problems/minimum-number-of-steps-to-make-two-strings-anagram/)
2+
3+
- 标签:哈希表、字符串、计数
4+
- 难度:中等
5+
6+
## 题目链接
7+
8+
- [1347. 制造字母异位词的最小步骤数 - 力扣](https://leetcode.cn/problems/minimum-number-of-steps-to-make-two-strings-anagram/)
9+
10+
## 题目大意
11+
12+
**描述**:给定两个长度相等的字符串 $s$ 和 $t$。每一个步骤中,你可以选择将 $t$ 中任一个字符替换为另一个字符。
13+
14+
**要求**:返回使 $t$ 成为 $s$ 的字母异位词的最小步骤数。
15+
16+
**说明**:
17+
18+
- **字母异位词**:指字母相同,但排列不同(也可能相同)的字符串。
19+
- 1ドル \le s.length \le 50000$。
20+
- $s.length == t.length$。
21+
- $s$ 和 $t$ 只包含小写英文字母。
22+
23+
**示例**:
24+
25+
- 示例 1:
26+
27+
```python
28+
输出:s = "bab", t = "aba"
29+
输出:1
30+
提示:用 'b' 替换 t 中的第一个 'a',t = "bba" 是 s 的一个字母异位词。
31+
```
32+
33+
- 示例 2:
34+
35+
```python
36+
输出:s = "leetcode", t = "practice"
37+
输出:5
38+
提示:用合适的字符替换 t 中的 'p', 'r', 'a', 'i''c',使 t 变成 s 的字母异位词。
39+
```
40+
41+
## 解题思路
42+
43+
### 思路 1:哈希表
44+
45+
题目要求使 $t$ 成为 $s$ 的字母异位词,则只需要 $t$ 和 $s$ 对应的每种字符数量相一致即可,无需考虑字符位置。
46+
47+
因为每一次转换都会减少一个字符,并增加另一个字符。
48+
49+
1. 我们使用两个哈希表 $cnts\underline{}s$、$cnts\underline{}t$ 分别对 $t$ 和 $s$ 中的字符进行计数,并求出两者的交集。
50+
2. 遍历交集中的字符种类,以及对应的字符数量。
51+
3. 对于当前字符 $key,ドル如果当前字符串 $s$ 中的字符 $key$ 的数量小于字符串 $t$ 中字符 $key$ 的数量,即 $cnts\underline{}s[key] < cnts\underline{}t[key]$。则 $s$ 中需要补齐的字符数量就是需要的最小步数,将其累加到答案中。
52+
4. 遍历完返回答案。
53+
54+
### 思路 1:代码
55+
56+
```Python
57+
class Solution:
58+
def minSteps(self, s: str, t: str) -> int:
59+
cnts_s, cnts_t = Counter(s), Counter(t)
60+
cnts = cnts_s | cnts_t
61+
62+
ans = 0
63+
for key, cnt in cnts.items():
64+
if cnts_s[key] < cnts_t[key]:
65+
ans += cnts_t[key] - cnts_s[key]
66+
67+
return ans
68+
```
69+
70+
### 思路 1:复杂度分析
71+
72+
- **时间复杂度**:$O(m + n),ドル其中 $m$、$n$ 分别为字符串 $s$、$t$ 的长度。
73+
- **空间复杂度**:$O(|\sum|),ドル其中 $\sum$ 是字符集,本题中 $| \sum | = 26$。
74+

‎Solutions/1476. 子矩形查询.md

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
# [1476. 子矩形查询](https://leetcode.cn/problems/subrectangle-queries/)
2+
3+
- 标签:设计、数组、矩阵
4+
- 难度:中等
5+
6+
## 题目链接
7+
8+
- [1476. 子矩形查询 - 力扣](https://leetcode.cn/problems/subrectangle-queries/)
9+
10+
## 题目大意
11+
12+
**要求**:实现一个类 SubrectangleQueries,它的构造函数的参数是一个 $rows \times cols $的矩形(这里用整数矩阵表示),并支持以下两种操作:
13+
14+
1. `updateSubrectangle(int row1, int col1, int row2, int col2, int newValue)`:用 $newValue$ 更新以 $(row1,col1)$ 为左上角且以 $(row2,col2)$ 为右下角的子矩形。
15+
16+
2. `getValue(int row, int col)`:返回矩形中坐标 (row,col) 的当前值。
17+
18+
**说明**:
19+
20+
- 最多有 500ドル$ 次 `updateSubrectangle``getValue` 操作。
21+
- 1ドル <= rows, cols <= 100$。
22+
- $rows == rectangle.length$。
23+
- $cols == rectangle[i].length$。
24+
- 0ドル <= row1 <= row2 < rows$。
25+
- 0ドル <= col1 <= col2 < cols$。
26+
- 1ドル <= newValue, rectangle[i][j] <= 10^9$。
27+
- 0ドル <= row < rows$。
28+
- 0ドル <= col < cols$。
29+
30+
**示例**:
31+
32+
- 示例 1:
33+
34+
```python
35+
输入:
36+
["SubrectangleQueries","getValue","updateSubrectangle","getValue","getValue","updateSubrectangle","getValue","getValue"]
37+
[[[[1,2,1],[4,3,4],[3,2,1],[1,1,1]]],[0,2],[0,0,3,2,5],[0,2],[3,1],[3,0,3,2,10],[3,1],[0,2]]
38+
输出:
39+
[null,1,null,5,5,null,10,5]
40+
解释:
41+
SubrectangleQueries subrectangleQueries = new SubrectangleQueries([[1,2,1],[4,3,4],[3,2,1],[1,1,1]]);
42+
// 初始的 (4x3) 矩形如下:
43+
// 1 2 1
44+
// 4 3 4
45+
// 3 2 1
46+
// 1 1 1
47+
subrectangleQueries.getValue(0, 2); // 返回 1
48+
subrectangleQueries.updateSubrectangle(0, 0, 3, 2, 5);
49+
// 此次更新后矩形变为:
50+
// 5 5 5
51+
// 5 5 5
52+
// 5 5 5
53+
// 5 5 5
54+
subrectangleQueries.getValue(0, 2); // 返回 5
55+
subrectangleQueries.getValue(3, 1); // 返回 5
56+
subrectangleQueries.updateSubrectangle(3, 0, 3, 2, 10);
57+
// 此次更新后矩形变为:
58+
// 5 5 5
59+
// 5 5 5
60+
// 5 5 5
61+
// 10 10 10
62+
subrectangleQueries.getValue(3, 1); // 返回 10
63+
subrectangleQueries.getValue(0, 2); // 返回 5
64+
```
65+
66+
- 示例 2:
67+
68+
```python
69+
输入:
70+
["SubrectangleQueries","getValue","updateSubrectangle","getValue","getValue","updateSubrectangle","getValue"]
71+
[[[[1,1,1],[2,2,2],[3,3,3]]],[0,0],[0,0,2,2,100],[0,0],[2,2],[1,1,2,2,20],[2,2]]
72+
输出:
73+
[null,1,null,100,100,null,20]
74+
解释:
75+
SubrectangleQueries subrectangleQueries = new SubrectangleQueries([[1,1,1],[2,2,2],[3,3,3]]);
76+
subrectangleQueries.getValue(0, 0); // 返回 1
77+
subrectangleQueries.updateSubrectangle(0, 0, 2, 2, 100);
78+
subrectangleQueries.getValue(0, 0); // 返回 100
79+
subrectangleQueries.getValue(2, 2); // 返回 100
80+
subrectangleQueries.updateSubrectangle(1, 1, 2, 2, 20);
81+
subrectangleQueries.getValue(2, 2); // 返回 20
82+
83+
```
84+
85+
## 解题思路
86+
87+
### 思路 1:暴力
88+
89+
矩形最大为 $row \times col == 100 \times 100,ドル则每次更新最多需要更新 10000ドル$ 个值,更新次数最多为 500ドル$ 次。
90+
91+
用暴力更新的方法最多需要更新 5000000ドル$ 次,我们可以尝试一下用暴力更新的方法解决本题(提交后发现可以通过)。
92+
93+
### 思路 1:代码
94+
95+
```Python
96+
class SubrectangleQueries:
97+
98+
def __init__(self, rectangle: List[List[int]]):
99+
self.rectangle = rectangle
100+
101+
102+
def updateSubrectangle(self, row1: int, col1: int, row2: int, col2: int, newValue: int) -> None:
103+
for row in range(row1, row2 + 1):
104+
for col in range(col1, col2 + 1):
105+
self.rectangle[row][col] = newValue
106+
107+
108+
def getValue(self, row: int, col: int) -> int:
109+
return self.rectangle[row][col]
110+
```
111+
112+
### 思路 1:复杂度分析
113+
114+
- **时间复杂度**:$O(row \times col \times 500)$。
115+
- **空间复杂度**:$O(row \times col)$。

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /