-
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
4 changed files
with
190 additions
and
2 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
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)$。 |
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,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)$。 | ||
|