-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
363 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
# [1324. 竖直打印单词](https://leetcode.cn/problems/print-words-vertically/) | ||
|
||
- 标签:数组、字符串、模拟 | ||
- 难度:中等 | ||
|
||
## 题目链接 | ||
|
||
- [1324. 竖直打印单词 - 力扣](https://leetcode.cn/problems/print-words-vertically/) | ||
|
||
## 题目大意 | ||
|
||
**描述**:给定一个字符串 $s$。 | ||
|
||
**要求**:按照单词在 $s$ 中出现顺序将它们全部竖直返回。 | ||
|
||
**说明**: | ||
|
||
- 单词应该以字符串列表的形式返回,必要时用空格补位,但输出尾部的空格需要删除(不允许尾随空格)。 | ||
- 每个单词只能放在一列上,每一列中也只能有一个单词。 | ||
- $1 \le s.length \le 200$。 | ||
- $s$ 仅含大写英文字母。 | ||
- 题目数据保证两个单词之间只有一个空格。 | ||
|
||
**示例**: | ||
|
||
- 示例 1: | ||
|
||
```python | ||
输入:s = "HOW ARE YOU" | ||
输出:["HAY","ORO","WEU"] | ||
解释:每个单词都应该竖直打印。 | ||
"HAY" | ||
"ORO" | ||
"WEU" | ||
``` | ||
|
||
- 示例 2: | ||
|
||
```python | ||
输入:s = "TO BE OR NOT TO BE" | ||
输出:["TBONTB","OEROOE"," T"] | ||
解释:题目允许使用空格补位,但不允许输出末尾出现空格。 | ||
"TBONTB" | ||
"OEROOE" | ||
" T" | ||
``` | ||
|
||
## 解题思路 | ||
|
||
### 思路 1:模拟 | ||
|
||
1. 将字符串 $s$ 按空格分割为单词数组 $words$。 | ||
2. 计算出单词数组 $words$ 中单词的最大长度 $max\underline{}len$。 | ||
3. 第一重循环遍历竖直单词的每个单词位置 $i$,第二重循环遍历当前第 $j$ 个单词。 | ||
1. 如果当前单词没有第 $i$ 个字符(当前单词的长度超过了单词位置 $i$),则将空格插入到竖直单词中。 | ||
2. 如果当前单词有第 $i$ 个字符,泽讲当前单词的第 $i$ 个字符插入到竖直单词中。 | ||
4. 第二重循环遍历完,将竖直单词去除尾随空格,并加入到答案数组中。 | ||
5. 第一重循环遍历完,则返回答案数组。 | ||
|
||
### 思路 1:代码 | ||
|
||
```Python | ||
class Solution: | ||
def printVertically(self, s: str) -> List[str]: | ||
words = s.split(' ') | ||
max_len = 0 | ||
for word in words: | ||
max_len = max(len(word), max_len) | ||
|
||
res = [] | ||
for i in range(max_len): | ||
ans = "" | ||
for j in range(len(words)): | ||
if i + 1 > len(words[j]): | ||
ans += ' ' | ||
else: | ||
ans += words[j][i] | ||
res.append(ans.rstrip()) | ||
|
||
return res | ||
``` | ||
|
||
### 思路 1:复杂度分析 | ||
|
||
- **时间复杂度**:$O(n \times max(|word|))$,其中 $n$ 为字符串 $s$ 中的单词个数,$max(|word|)$ 是最长的单词长度。。 | ||
- **空间复杂度**:$O(n \times max(|word|))$。 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
# [1347. 制造字母异位词的最小步骤数](https://leetcode.cn/problems/minimum-number-of-steps-to-make-two-strings-anagram/) | ||
|
||
- 标签:哈希表、字符串、计数 | ||
- 难度:中等 | ||
|
||
## 题目链接 | ||
|
||
- [1347. 制造字母异位词的最小步骤数 - 力扣](https://leetcode.cn/problems/minimum-number-of-steps-to-make-two-strings-anagram/) | ||
|
||
## 题目大意 | ||
|
||
**描述**:给定两个长度相等的字符串 $s$ 和 $t$。每一个步骤中,你可以选择将 $t$ 中任一个字符替换为另一个字符。 | ||
|
||
**要求**:返回使 $t$ 成为 $s$ 的字母异位词的最小步骤数。 | ||
|
||
**说明**: | ||
|
||
- **字母异位词**:指字母相同,但排列不同(也可能相同)的字符串。 | ||
- $1 \le s.length \le 50000$。 | ||
- $s.length == t.length$。 | ||
- $s$ 和 $t$ 只包含小写英文字母。 | ||
|
||
**示例**: | ||
|
||
- 示例 1: | ||
|
||
```python | ||
输出:s = "bab", t = "aba" | ||
输出:1 | ||
提示:用 'b' 替换 t 中的第一个 'a',t = "bba" 是 s 的一个字母异位词。 | ||
``` | ||
|
||
- 示例 2: | ||
|
||
```python | ||
输出:s = "leetcode", t = "practice" | ||
输出:5 | ||
提示:用合适的字符替换 t 中的 'p', 'r', 'a', 'i' 和 'c',使 t 变成 s 的字母异位词。 | ||
``` | ||
|
||
## 解题思路 | ||
|
||
### 思路 1:哈希表 | ||
|
||
题目要求使 $t$ 成为 $s$ 的字母异位词,则只需要 $t$ 和 $s$ 对应的每种字符数量相一致即可,无需考虑字符位置。 | ||
|
||
因为每一次转换都会减少一个字符,并增加另一个字符。 | ||
|
||
1. 我们使用两个哈希表 $cnts\underline{}s$、$cnts\underline{}t$ 分别对 $t$ 和 $s$ 中的字符进行计数,并求出两者的交集。 | ||
2. 遍历交集中的字符种类,以及对应的字符数量。 | ||
3. 对于当前字符 $key$,如果当前字符串 $s$ 中的字符 $key$ 的数量小于字符串 $t$ 中字符 $key$ 的数量,即 $cnts\underline{}s[key] < cnts\underline{}t[key]$。则 $s$ 中需要补齐的字符数量就是需要的最小步数,将其累加到答案中。 | ||
4. 遍历完返回答案。 | ||
|
||
### 思路 1:代码 | ||
|
||
```Python | ||
class Solution: | ||
def minSteps(self, s: str, t: str) -> int: | ||
cnts_s, cnts_t = Counter(s), Counter(t) | ||
cnts = cnts_s | cnts_t | ||
|
||
ans = 0 | ||
for key, cnt in cnts.items(): | ||
if cnts_s[key] < cnts_t[key]: | ||
ans += cnts_t[key] - cnts_s[key] | ||
|
||
return ans | ||
``` | ||
|
||
### 思路 1:复杂度分析 | ||
|
||
- **时间复杂度**:$O(m + n)$,其中 $m$、$n$ 分别为字符串 $s$、$t$ 的长度。 | ||
- **空间复杂度**:$O(|\sum|)$,其中 $\sum$ 是字符集,本题中 $| \sum | = 26$。 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
# [1476. 子矩形查询](https://leetcode.cn/problems/subrectangle-queries/) | ||
|
||
- 标签:设计、数组、矩阵 | ||
- 难度:中等 | ||
|
||
## 题目链接 | ||
|
||
- [1476. 子矩形查询 - 力扣](https://leetcode.cn/problems/subrectangle-queries/) | ||
|
||
## 题目大意 | ||
|
||
**要求**:实现一个类 SubrectangleQueries,它的构造函数的参数是一个 $rows \times cols $的矩形(这里用整数矩阵表示),并支持以下两种操作: | ||
|
||
1. `updateSubrectangle(int row1, int col1, int row2, int col2, int newValue)`:用 $newValue$ 更新以 $(row1,col1)$ 为左上角且以 $(row2,col2)$ 为右下角的子矩形。 | ||
|
||
2. `getValue(int row, int col)`:返回矩形中坐标 (row,col) 的当前值。 | ||
|
||
**说明**: | ||
|
||
- 最多有 $500$ 次 `updateSubrectangle` 和 `getValue` 操作。 | ||
- $1 <= rows, cols <= 100$。 | ||
- $rows == rectangle.length$。 | ||
- $cols == rectangle[i].length$。 | ||
- $0 <= row1 <= row2 < rows$。 | ||
- $0 <= col1 <= col2 < cols$。 | ||
- $1 <= newValue, rectangle[i][j] <= 10^9$。 | ||
- $0 <= row < rows$。 | ||
- $0 <= col < cols$。 | ||
|
||
**示例**: | ||
|
||
- 示例 1: | ||
|
||
```python | ||
输入: | ||
["SubrectangleQueries","getValue","updateSubrectangle","getValue","getValue","updateSubrectangle","getValue","getValue"] | ||
[[[[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]] | ||
输出: | ||
[null,1,null,5,5,null,10,5] | ||
解释: | ||
SubrectangleQueries subrectangleQueries = new SubrectangleQueries([[1,2,1],[4,3,4],[3,2,1],[1,1,1]]); | ||
// 初始的 (4x3) 矩形如下: | ||
// 1 2 1 | ||
// 4 3 4 | ||
// 3 2 1 | ||
// 1 1 1 | ||
subrectangleQueries.getValue(0, 2); // 返回 1 | ||
subrectangleQueries.updateSubrectangle(0, 0, 3, 2, 5); | ||
// 此次更新后矩形变为: | ||
// 5 5 5 | ||
// 5 5 5 | ||
// 5 5 5 | ||
// 5 5 5 | ||
subrectangleQueries.getValue(0, 2); // 返回 5 | ||
subrectangleQueries.getValue(3, 1); // 返回 5 | ||
subrectangleQueries.updateSubrectangle(3, 0, 3, 2, 10); | ||
// 此次更新后矩形变为: | ||
// 5 5 5 | ||
// 5 5 5 | ||
// 5 5 5 | ||
// 10 10 10 | ||
subrectangleQueries.getValue(3, 1); // 返回 10 | ||
subrectangleQueries.getValue(0, 2); // 返回 5 | ||
``` | ||
|
||
- 示例 2: | ||
|
||
```python | ||
输入: | ||
["SubrectangleQueries","getValue","updateSubrectangle","getValue","getValue","updateSubrectangle","getValue"] | ||
[[[[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]] | ||
输出: | ||
[null,1,null,100,100,null,20] | ||
解释: | ||
SubrectangleQueries subrectangleQueries = new SubrectangleQueries([[1,1,1],[2,2,2],[3,3,3]]); | ||
subrectangleQueries.getValue(0, 0); // 返回 1 | ||
subrectangleQueries.updateSubrectangle(0, 0, 2, 2, 100); | ||
subrectangleQueries.getValue(0, 0); // 返回 100 | ||
subrectangleQueries.getValue(2, 2); // 返回 100 | ||
subrectangleQueries.updateSubrectangle(1, 1, 2, 2, 20); | ||
subrectangleQueries.getValue(2, 2); // 返回 20 | ||
|
||
``` | ||
|
||
## 解题思路 | ||
|
||
### 思路 1:暴力 | ||
|
||
矩形最大为 $row \times col == 100 \times 100$,则每次更新最多需要更新 $10000$ 个值,更新次数最多为 $500$ 次。 | ||
|
||
用暴力更新的方法最多需要更新 $5000000$ 次,我们可以尝试一下用暴力更新的方法解决本题(提交后发现可以通过)。 | ||
|
||
### 思路 1:代码 | ||
|
||
```Python | ||
class SubrectangleQueries: | ||
|
||
def __init__(self, rectangle: List[List[int]]): | ||
self.rectangle = rectangle | ||
|
||
|
||
def updateSubrectangle(self, row1: int, col1: int, row2: int, col2: int, newValue: int) -> None: | ||
for row in range(row1, row2 + 1): | ||
for col in range(col1, col2 + 1): | ||
self.rectangle[row][col] = newValue | ||
|
||
|
||
def getValue(self, row: int, col: int) -> int: | ||
return self.rectangle[row][col] | ||
``` | ||
|
||
### 思路 1:复杂度分析 | ||
|
||
- **时间复杂度**:$O(row \times col \times 500)$。 | ||
- **空间复杂度**:$O(row \times col)$。 |
Oops, something went wrong.