Skip to content

Commit

Permalink
更新题解列表
Browse files Browse the repository at this point in the history
  • Loading branch information
itcharge committed Jan 16, 2024
1 parent 8ea6473 commit a514b48
Show file tree
Hide file tree
Showing 4 changed files with 190 additions and 2 deletions.
4 changes: 3 additions & 1 deletion Contents/00.Introduction/04.Solutions-List.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# LeetCode 题解(已完成 850 道)
# LeetCode 题解(已完成 852 道)

| 题号 | 标题 | 题解 | 标签 | 难度 |
| :------ | :------ | :------ | :------ | :------ |
Expand Down Expand Up @@ -512,6 +512,7 @@
| 1037 | [有效的回旋镖](https://leetcode.cn/problems/valid-boomerang/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1037.%20%E6%9C%89%E6%95%88%E7%9A%84%E5%9B%9E%E6%97%8B%E9%95%96.md) | 几何、数组、数学 | 简单 |
| 1038 | [从二叉搜索树到更大和树](https://leetcode.cn/problems/binary-search-tree-to-greater-sum-tree/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1038.%20%E4%BB%8E%E4%BA%8C%E5%8F%89%E6%90%9C%E7%B4%A2%E6%A0%91%E5%88%B0%E6%9B%B4%E5%A4%A7%E5%92%8C%E6%A0%91.md) | 树、深度优先搜索、二叉搜索树、二叉树 | 中等 |
| 1039 | [多边形三角剖分的最低得分](https://leetcode.cn/problems/minimum-score-triangulation-of-polygon/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1039.%20%E5%A4%9A%E8%BE%B9%E5%BD%A2%E4%B8%89%E8%A7%92%E5%89%96%E5%88%86%E7%9A%84%E6%9C%80%E4%BD%8E%E5%BE%97%E5%88%86.md) | 数组、动态规划 | 中等 |
| 1041 | [困于环中的机器人](https://leetcode.cn/problems/robot-bounded-in-circle/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1041.%20%E5%9B%B0%E4%BA%8E%E7%8E%AF%E4%B8%AD%E7%9A%84%E6%9C%BA%E5%99%A8%E4%BA%BA.md) | 数学、字符串、模拟 | 中等 |
| 1047 | [删除字符串中的所有相邻重复项](https://leetcode.cn/problems/remove-all-adjacent-duplicates-in-string/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1047.%20%E5%88%A0%E9%99%A4%E5%AD%97%E7%AC%A6%E4%B8%B2%E4%B8%AD%E7%9A%84%E6%89%80%E6%9C%89%E7%9B%B8%E9%82%BB%E9%87%8D%E5%A4%8D%E9%A1%B9.md) | 栈、字符串 | 简单 |
| 1049 | [最后一块石头的重量 II](https://leetcode.cn/problems/last-stone-weight-ii/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1049.%20%E6%9C%80%E5%90%8E%E4%B8%80%E5%9D%97%E7%9F%B3%E5%A4%B4%E7%9A%84%E9%87%8D%E9%87%8F%20II.md) | 数组、动态规划 | 中等 |
| 1051 | [高度检查器](https://leetcode.cn/problems/height-checker/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1051.%20%E9%AB%98%E5%BA%A6%E6%A3%80%E6%9F%A5%E5%99%A8.md) | 数组、计数排序、排序 | 简单 |
Expand Down Expand Up @@ -612,6 +613,7 @@
| 1736 | [替换隐藏数字得到的最晚时间](https://leetcode.cn/problems/latest-time-by-replacing-hidden-digits/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1736.%20%E6%9B%BF%E6%8D%A2%E9%9A%90%E8%97%8F%E6%95%B0%E5%AD%97%E5%BE%97%E5%88%B0%E7%9A%84%E6%9C%80%E6%99%9A%E6%97%B6%E9%97%B4.md) | 贪心、字符串 | 简单 |
| 1742 | [盒子中小球的最大数量](https://leetcode.cn/problems/maximum-number-of-balls-in-a-box/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1742.%20%E7%9B%92%E5%AD%90%E4%B8%AD%E5%B0%8F%E7%90%83%E7%9A%84%E6%9C%80%E5%A4%A7%E6%95%B0%E9%87%8F.md) | 哈希表、数学、计数 | 简单 |
| 1749 | [任意子数组和的绝对值的最大值](https://leetcode.cn/problems/maximum-absolute-sum-of-any-subarray/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1749.%20%E4%BB%BB%E6%84%8F%E5%AD%90%E6%95%B0%E7%BB%84%E5%92%8C%E7%9A%84%E7%BB%9D%E5%AF%B9%E5%80%BC%E7%9A%84%E6%9C%80%E5%A4%A7%E5%80%BC.md) | 数组、动态规划 | 中等 |
| 1763 | [最长的美好子字符串](https://leetcode.cn/problems/longest-nice-substring/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1763.%20%E6%9C%80%E9%95%BF%E7%9A%84%E7%BE%8E%E5%A5%BD%E5%AD%90%E5%AD%97%E7%AC%A6%E4%B8%B2.md) | 位运算、哈希表、字符串、分治、滑动窗口 | 简单 |
| 1779 | [找到最近的有相同 X 或 Y 坐标的点](https://leetcode.cn/problems/find-nearest-point-that-has-the-same-x-or-y-coordinate/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1779.%20%E6%89%BE%E5%88%B0%E6%9C%80%E8%BF%91%E7%9A%84%E6%9C%89%E7%9B%B8%E5%90%8C%20X%20%E6%88%96%20Y%20%E5%9D%90%E6%A0%87%E7%9A%84%E7%82%B9.md) | 数组 | 简单 |
| 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) || 简单 |
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -255,4 +255,4 @@
- [动态规划优化题目](./Contents/10.Dynamic-Programming/11.DP-Optimization/04.DP-Optimization-List.md)

## 11. 附加内容
## [12. LeetCode 题解(已完成 850 道)](./Contents/00.Introduction/04.Solutions-List.md)
## [12. LeetCode 题解(已完成 852 道)](./Contents/00.Introduction/04.Solutions-List.md)
106 changes: 106 additions & 0 deletions Solutions/1041. 困于环中的机器人.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
# [1041. 困于环中的机器人](https://leetcode.cn/problems/robot-bounded-in-circle/)

- 标签:数学、字符串、模拟
- 难度:中等

## 题目链接

- [1041. 困于环中的机器人 - 力扣](https://leetcode.cn/problems/robot-bounded-in-circle/)

## 题目大意

**描述**:在无限的平面上,机器人最初位于 $(0, 0)$ 处,面朝北方。注意:

- 北方向 是 $y$ 轴的正方向。
- 南方向 是 $y$ 轴的负方向。
- 东方向 是 $x$ 轴的正方向。
- 西方向 是 $x$ 轴的负方向。

机器人可以接受下列三条指令之一:

- `"G"`:直走 $1$ 个单位
- `"L"`:左转 $90$ 度
- `"R"`:右转 $90$ 度

给定一个字符串 $instructions$,机器人按顺序执行指令 $instructions$,并一直重复它们。

**要求**:只有在平面中存在环使得机器人永远无法离开时,返回 $True$。否则,返回 $False$。

**说明**

- $1 \le instructions.length \le 100$。
- $instructions[i]$ 仅包含 `'G'``'L'``'R'`

**示例**

- 示例 1:

```python
输入:instructions = "GGLLGG"
输出:True
解释:机器人最初在(0,0)处,面向北方。
“G”:移动一步。位置:(0,1)方向:北。
“G”:移动一步。位置:(0,2).方向:北。
“L”:逆时针旋转90度。位置:(0,2).方向:西。
“L”:逆时针旋转90度。位置:(0,2)方向:南。
“G”:移动一步。位置:(0,1)方向:南。
“G”:移动一步。位置:(0,0)方向:南。
重复指令,机器人进入循环:(0,0)——>(0,1)——>(0,2)——>(0,1)——>(0,0)。
在此基础上,我们返回 True
```

- 示例 2:

```python
输入:instructions = "GG"
输出:False
解释:机器人最初在(0,0)处,面向北方。
“G”:移动一步。位置:(0,1)方向:北。
“G”:移动一步。位置:(0,2).方向:北。
重复这些指示,继续朝北前进,不会进入循环。
在此基础上,返回 False
```

## 解题思路

### 思路 1:模拟

设定初始位置为 $(0, 0)$,初始方向 $direction = 0$,假设按照给定字符串 $instructions$ 执行一遍之后,位于 $(x, y)$ 处,且方向为 $direction$,则可能出现的所有情况为:

1. 方向不变($direction == 0$),且 $(x, y) == (0, 0)$,则会一直在原点,无法走出去。
2. 方向不变($direction == 0$),且 $(x, y) \ne (0, 0)$,则可以走出去。
3. 方向相反($direction == 2$),无论是否产生位移,则再执行 $1$ 遍将会回到原点。
4. 方向逆时针 / 顺时针改变 $90°$($direction == 1 \text{ or } 3$),无论是否产生位移,则再执行 $3$ 遍将会回到原点。

综上所述,最多模拟 $4$ 次即可知道能否回到原点。

从上面也可以等出结论:如果不产生位移,则一定会回到原点。如果改变方向,同样一定会回到原点。

我们只需要根据以上结论,按照 $instructions$ 执行一遍之后,通过判断是否产生位移和改变方向,即可判断是否一定会回到原点。

### 思路 1:代码

```Python
class Solution:
def isRobotBounded(self, instructions: str) -> bool:
# 分别代表北、东、南、西
directions = [(0, 1), (-1, 0), (0, -1), (1, 0)]
x, y = 0, 0
# 初始方向为北
direction = 0
for step in instructions:
if step == 'G':
x += directions[direction][0]
y += directions[direction][1]
elif step == 'L':
direction = (direction + 1) % 4
else:
direction = (direction + 3) % 4

return (x == 0 and y == 0) or direction != 0
```

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

- **时间复杂度**:$O(n)$,其中 $n$ 为字符串 $instructions$ 的长度。
- **空间复杂度**:$O(1)$。
80 changes: 80 additions & 0 deletions Solutions/1763. 最长的美好子字符串.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# [1763. 最长的美好子字符串](https://leetcode.cn/problems/longest-nice-substring/)

- 标签:位运算、哈希表、字符串、分治、滑动窗口
- 难度:简单

## 题目链接

- [1763. 最长的美好子字符串 - 力扣](https://leetcode.cn/problems/longest-nice-substring/)

## 题目大意

**描述**: 给定一个字符串 $s$。

**要求**:返回 $s$ 最长的美好子字符串。

**说明**

- **美好字符串**:当一个字符串 $s$ 包含的每一种字母的大写和小写形式同时出现在 $s$ 中,就称这个字符串 $s$ 是美好字符串。
- $1 \le s.length \le 100$。

**示例**

- 示例 1:

```python
输入:s = "YazaAay"
输出:"aAa"
解释:"aAa" 是一个美好字符串,因为这个子串中仅含一种字母,其小写形式 'a' 和大写形式 'A' 也同时出现了。
"aAa" 是最长的美好子字符串。
```

- 示例 2:

```python
输入:s = "Bb"
输出:"Bb"
解释:"Bb" 是美好字符串,因为 'B''b' 都出现了。整个字符串也是原字符串的子字符串。
```

## 解题思路

### 思路 1:枚举

字符串 $s$ 的范围为 $[1, 100]$,长度较小,我们可以枚举所有的子串,判断该子串是否为美好字符串。

由于大小写英文字母各有 $26$ 位,则我们可以利用二进制来标记某字符是否在子串中出现过,我们使用 $lower$ 标记子串中出现过的小写字母,使用 $upper$ 标记子串中出现过的大写字母。如果满足 $lower == upper$,则说明该子串为美好字符串。

具体解法步骤如下:

1. 使用二重循环遍历字符串。对于子串 $s[i]…s[j]$,使用 $lower$ 标记子串中出现过的小写字母,使用 $upper$ 标记子串中出现过的大写字母。
2. 如果 $s[j]$ 为小写字母,则 $lower$ 对应位置标记为出现过该小写字母,即:`lower |= 1 << (ord(s[j]) - ord('a'))`
3. 如果 $s[j]$ 为大写字母,则 $upper$ 对应位置标记为出现过该小写字母,即:`upper |= 1 << (ord(s[j]) - ord('A'))`
4. 判断当前子串对应 $lower$ 和 $upper$ 是否相等,如果相等,并且子串长度大于记录的最长美好字符串长度,则更新最长美好字符串长度。
5. 遍历完返回记录的最长美好字符串长度。

### 思路 1:代码

```Python
class Solution:
def longestNiceSubstring(self, s: str) -> str:
size = len(s)
max_pos, max_len = 0, 0
for i in range(size):
lower, upper = 0, 0
for j in range(i, size):
if s[j].islower():
lower |= 1 << (ord(s[j]) - ord('a'))
else:
upper |= 1 << (ord(s[j]) - ord('A'))
if lower == upper and j - i + 1 > max_len:
max_len = j - i + 1
max_pos = i
return s[max_pos: max_pos + max_len]
```

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

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

0 comments on commit a514b48

Please sign in to comment.