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

✨feat: add 943、950、985、1041、1222、1253 #773

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
SharingSource merged 1 commit into main from ac_oier
Jul 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Index/图论 BFS.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
| [1034. 边界着色](https://leetcode-cn.com/problems/coloring-a-border/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/coloring-a-border/solution/gong-shui-san-xie-tu-lun-sou-suo-zhuan-t-snvw/) | 中等 | 🤩🤩🤩🤩 |
| [1036. 逃离大迷宫](https://leetcode-cn.com/problems/escape-a-large-maze/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/escape-a-large-maze/solution/gong-shui-san-xie-bfs-gei-ding-zhang-ai-8w63o/) | 困难 | 🤩🤩🤩🤩 |
| [1162. 地图分析](https://leetcode-cn.com/problems/as-far-from-land-as-possible/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/as-far-from-land-as-possible/solution/gong-shui-san-xie-ru-he-shi-yong-duo-yua-vlea/) | 中等 | 🤩🤩🤩🤩 |
| [1222. 可以攻击国王的皇后](https://leetcode.cn/problems/queens-that-can-attack-the-king/) | [LeetCode 题解链接](https://leetcode.cn/problems/queens-that-can-attack-the-king/solution/gong-shui-san-xie-chang-gui-mo-ni-ti-by-hlgkx/) | 中等 | 🤩🤩🤩🤩 |
| [1345. 跳跃游戏 IV](https://leetcode-cn.com/problems/jump-game-iv/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/jump-game-iv/solution/gong-shui-san-xie-noxiang-xin-ke-xue-xi-q9tb1/) | 困难 | 🤩🤩🤩🤩🤩 |
| [1765. 地图中的最高点](https://leetcode-cn.com/problems/map-of-highest-peak/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/map-of-highest-peak/solution/gong-shui-san-xie-duo-yuan-bfs-yun-yong-8sw0f/) | 中等 | 🤩🤩🤩🤩 |
| [2039. 网络空闲的时刻](https://leetcode-cn.com/problems/the-time-when-the-network-becomes-idle/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/the-time-when-the-network-becomes-idle/solution/by-ac_oier-5txs/) | 中等 | 🤩🤩🤩 |
Expand Down
1 change: 1 addition & 0 deletions Index/构造.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
| [791. 自定义字符串排序](https://leetcode.cn/problems/custom-sort-string/) | [LeetCode 题解链接](https://leetcode.cn/problems/custom-sort-string/solution/by-ac_oier-ali0/) | 中等 | 🤩🤩🤩🤩🤩 |
| [899. 有序队列](https://leetcode.cn/problems/orderly-queue/) | [LeetCode 题解链接](https://leetcode.cn/problems/orderly-queue/solution/by-ac_oier-443m/) | 困难 | 🤩🤩🤩 |
| [942. 增减字符串匹配](https://leetcode.cn/problems/di-string-match/) | [LeetCode 题解链接](https://leetcode.cn/problems/di-string-match/solution/by-ac_oier-pvjk/) | 简单 | 🤩🤩🤩🤩🤩 |
| [950. 按递增顺序显示卡牌](https://leetcode.cn/problems/reveal-cards-in-increasing-order/) | [LeetCode 题解链接](https://leetcode.cn/problems/reveal-cards-in-increasing-order/solution/gong-shui-san-xie-jian-dan-mo-ni-ti-by-a-nu48/) | 中等 | 🤩🤩🤩🤩🤩 |
| [961. 在长度 2N 的数组中找出重复 N 次的元素](https://leetcode.cn/problems/n-repeated-element-in-size-2n-array/) | [LeetCode 题解链接](https://leetcode.cn/problems/n-repeated-element-in-size-2n-array/solution/by-ac_oier-bslq/) | 简单 | 🤩🤩🤩🤩 |
| [1092. 最短公共超序列](https://leetcode.cn/problems/shortest-common-supersequence/) | [LeetCode 题解链接](https://leetcode.cn/problems/shortest-common-supersequence/solution/by-ac_oier-s346/) | 困难 | 🤩🤩🤩🤩 |
| [1260. 二维网格迁移](https://leetcode.cn/problems/shift-2d-grid/) | [LeetCode 题解链接](https://leetcode.cn/problems/shift-2d-grid/solution/by-ac_oier-1blt/) | 简单 | 🤩🤩🤩🤩 |
Expand Down
3 changes: 3 additions & 0 deletions Index/模拟.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,10 @@
| [929. 独特的电子邮件地址](https://leetcode.cn/problems/unique-email-addresses/) | [LeetCode 题解链接](https://leetcode.cn/problems/unique-email-addresses/solution/by-ac_oier-d3zu/) | 简单 | 🤩🤩🤩 |
| [944. 删列造序](https://leetcode.cn/problems/delete-columns-to-make-sorted/) | [LeetCode 题解链接](https://leetcode.cn/problems/delete-columns-to-make-sorted/solution/by-ac_oier-smoz/) | 简单 | 🤩🤩🤩 |
| [946. 验证栈序列](https://leetcode.cn/problems/validate-stack-sequences/) | [LeetCode 题解链接](https://leetcode.cn/problems/validate-stack-sequences/solution/by-ac_oier-84qd/) | 中等 | 🤩🤩🤩🤩 |
| [950. 按递增顺序显示卡牌](https://leetcode.cn/problems/reveal-cards-in-increasing-order/) | [LeetCode 题解链接](https://leetcode.cn/problems/reveal-cards-in-increasing-order/solution/gong-shui-san-xie-jian-dan-mo-ni-ti-by-a-nu48/) | 中等 | 🤩🤩🤩🤩🤩 |
| [953. 验证外星语词典](https://leetcode.cn/problems/verifying-an-alien-dictionary/) | [LeetCode 题解链接](https://leetcode.cn/problems/verifying-an-alien-dictionary/solution/by-ac_oier-sxf1/) | 简单 | 🤩🤩🤩🤩 |
| [961. 在长度 2N 的数组中找出重复 N 次的元素](https://leetcode.cn/problems/n-repeated-element-in-size-2n-array/) | [LeetCode 题解链接](https://leetcode.cn/problems/n-repeated-element-in-size-2n-array/solution/by-ac_oier-bslq/) | 简单 | 🤩🤩🤩🤩 |
| [985. 查询后的偶数和](https://leetcode.cn/problems/sum-of-even-numbers-after-queries/) | [LeetCode 题解链接](https://leetcode.cn/problems/sum-of-even-numbers-after-queries/solution/gong-shui-san-xie-jian-dan-mo-ni-ti-by-a-lwfq/) | 中等 | 🤩🤩🤩🤩 |
| [997. 找到小镇的法官](https://leetcode-cn.com/problems/find-the-town-judge/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/find-the-town-judge/solution/gong-shui-san-xie-jian-dan-chu-du-ru-du-5ms57/) | 简单 | 🤩🤩🤩🤩 |
| [998. 最大二叉树 II](https://leetcode.cn/problems/maximum-binary-tree-ii/) | [LeetCode 题解链接](https://leetcode.cn/problems/maximum-binary-tree-ii/solution/by-ac_oier-v82s/) | 中等 | 🤩🤩🤩🤩 |
| [1001. 网格照明](https://leetcode-cn.com/problems/grid-illumination/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/grid-illumination/solution/gong-shui-san-xie-ha-xi-biao-xian-ying-s-s48d/) | 困难 | 🤩🤩🤩🤩 |
Expand All @@ -152,6 +154,7 @@
| [1184. 公交站间的距离](https://leetcode.cn/problems/distance-between-bus-stops/) | [LeetCode 题解链接](https://leetcode.cn/problems/distance-between-bus-stops/solution/by-ac_oier-fow3/) | 简单 | 🤩🤩🤩🤩 |
| [1185. 一周中的第几天](https://leetcode-cn.com/problems/day-of-the-week/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/day-of-the-week/solution/gong-shui-san-xie-jian-dan-ri-qi-tong-ji-czt6/) | 简单 | 🤩🤩🤩🤩 |
| [1189. "气球" 的最大数量](https://leetcode-cn.com/problems/maximum-number-of-balloons/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/maximum-number-of-balloons/solution/gong-shui-san-xie-jian-dan-mo-ni-ti-by-a-9px4/) | 简单 | 🤩🤩🤩🤩 |
| [1222. 可以攻击国王的皇后](https://leetcode.cn/problems/queens-that-can-attack-the-king/) | [LeetCode 题解链接](https://leetcode.cn/problems/queens-that-can-attack-the-king/solution/gong-shui-san-xie-chang-gui-mo-ni-ti-by-hlgkx/) | 中等 | 🤩🤩🤩🤩 |
| [1224. 最大相等频率](https://leetcode.cn/problems/maximum-equal-frequency/) | [LeetCode 题解链接](https://leetcode.cn/problems/maximum-equal-frequency/solution/by-ac_oier-fviv/) | 困难 | 🤩🤩🤩🤩 |
| [1252. 奇数值单元格的数目](https://leetcode.cn/problems/cells-with-odd-values-in-a-matrix/) | [LeetCode 题解链接](https://leetcode.cn/problems/cells-with-odd-values-in-a-matrix/solution/by-ac_oier-p0za/) | 简单 | 🤩🤩🤩 |
| [1260. 二维网格迁移](https://leetcode.cn/problems/shift-2d-grid/) | [LeetCode 题解链接](https://leetcode.cn/problems/shift-2d-grid/solution/by-ac_oier-1blt/) | 简单 | 🤩🤩🤩🤩 |
Expand Down
1 change: 1 addition & 0 deletions Index/状压 DP.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
| [526. 优美的排列](https://leetcode-cn.com/problems/beautiful-arrangement/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/beautiful-arrangement/solution/gong-shui-san-xie-xiang-jie-liang-chong-vgsia/) | 中等 | 🤩🤩🤩🤩🤩 |
| [691. 贴纸拼词](https://leetcode.cn/problems/stickers-to-spell-word/) | [LeetCode 题解链接](https://leetcode.cn/problems/stickers-to-spell-word/solution/by-ac_oier-5vv3/) | 困难 | 🤩🤩🤩🤩 |
| [847. 访问所有节点的最短路径](https://leetcode-cn.com/problems/shortest-path-visiting-all-nodes/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/shortest-path-visiting-all-nodes/solution/gong-shui-san-xie-yi-ti-shuang-jie-bfs-z-6p2k/) | 困难 | 🤩🤩🤩🤩🤩 |
| [943. 最短超级串](https://leetcode.cn/problems/find-the-shortest-superstring/) | [LeetCode 题解链接](https://leetcode.cn/problems/find-the-shortest-superstring/solution/gong-shui-san-xie-zhuang-ya-dp-yun-yong-p6hlz/) | 困难 | 🤩🤩🤩🤩🤩 |
| [1994. 好子集的数目](https://leetcode-cn.com/problems/the-number-of-good-subsets/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/the-number-of-good-subsets/solution/gong-shui-san-xie-zhuang-ya-dp-yun-yong-gz4w5/) | 困难 | 🤩🤩🤩🤩 |
| [2044. 统计按位或能得到最大值的子集数目](https://leetcode-cn.com/problems/count-number-of-maximum-bitwise-or-subsets/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/count-number-of-maximum-bitwise-or-subsets/solution/by-ac_oier-dos6/) | 困难 | 🤩🤩🤩🤩 |

216 changes: 216 additions & 0 deletions LeetCode/1041-1050/1041. 困于环中的机器人(中等).md
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,216 @@
### 题目描述

这是 LeetCode 上的 **[1041. 困于环中的机器人](https://leetcode.cn/problems/robot-bounded-in-circle/solution/gong-shui-san-xie-chang-gui-mo-ni-ti-by-hgdtp/)** ,难度为 **中等**。

Tag : 「模拟」、「脑筋急转弯」



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

* 北方向 是 `y` 轴的正方向。
* 南方向 是 `y` 轴的负方向。
* 东方向 是 `x` 轴的正方向。
* 西方向 是 `x` 轴的负方向。

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

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

机器人按顺序执行指令 `instructions`,并一直重复它们。

只有在平面中存在环使得机器人永远无法离开时,返回 `true`。否则,返回 `false`。

示例 1:
```
输入: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:
```
输入:instructions = "GG"

输出:false

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

输出:true

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

提示:
* 1ドル <= instructions.length <= 100$
* `instructions[i]` 仅包含 `'G'`, `'L'`, `'R'`

---

### 模拟

为了方便,将 `instructions` 记为 `s`,"北西南东"四个方向分别记为"上左下右"四个逆时针方向。

起始位置在 $(0,0),ドル方向为上,我们可以将「位置 + 方向」统称为「状态」。

所谓"循环",则是指执行若干次的 `s` 后,会回到相同的状态。

我们可以按 `s` 执行一遍,假设执行完所在位置为 $(x, y),ドル所在位置为 $k,ドル先根据 **位置** 分情况讨论:

1. $(x, y)$ 为 $(0, 0),ドル此时无论执行一遍后的方向为何值,必然能在若干次执行后回到起始状态。

即只需要确保 `(n * k) % 4` 为 0ドル$ 即可,机器人会陷入循环;

2. $(x, y)$ 不为 $(0, 0),ドル再根据 **方向** 进一步分情况讨论:

* 方向为上:每执行一遍 `s` 指令,位置变化为 $(x, y),ドル方向不变。

那么执行 $n$ 遍后位置为 $(n \times x, n \times y),ドル其中 $n$ 为正整数,并且 $x$ 和 $y$ 不同时为 0ドル,ドル因此随着执行次数增加,位置会离 $(0, 0)$ 越来越远,机器人不会陷入循环;

* 方向为下:每执行一遍 `s` 指令,位置变化为 $(x, y),ドル方向翻转。

如果再执行一遍,由于再次执行时的方向与起始方向相反,因此位置变化为 $(-x, -y),ドル同时方向再次翻转(与起始方向一致)。即执行偶数次后,会回到起始状态,机器人会陷入循环;

* 方向为左:每执行一遍 `s` 指令,位置变化为 $(x, y),ドル方向为逆时针 90ドル$ 度。

如果执行第二次,位置变化为 $(-y, x),ドル方向再逆时针 90ドル$ 度(与起始方向相反);执行第三次,位置变化为 $(-x, -y),ドル方向再逆时针 90ドル$ 度(与起始方向呈顺时针 90ドル$ 度),该次变化会和首次执行相互抵消;执行第四次,位置变化为 $(y, -x),ドル方向再逆时针 90ドル$ 度(与起始方向相同),该次变化与第二次执行相互抵消。总的位置变化为 $(0, 0),ドル同时方向与起始方向一致,机器人会陷入循环;

* 方向为右:与「方向为左」同理,机器人会陷入循环。

综上,只要执行一遍 `s` 后所在位置为 $(0, 0)$ 或方向不为上,均可确保循环发生。

Java 代码:
```Java
class Solution {
public boolean isRobotBounded(String s) {
int x = 0, y = 0, d = 0;
int[][] dirs = new int[][]{{0,1}, {-1,0}, {0,-1}, {1,0}};
for (char c : s.toCharArray()) {
if (c == 'G') {
x += dirs[d][0]; y += dirs[d][1];
} else if (c == 'L') {
d = (d + 1) % 4;
} else {
d = ((d - 1) % 4 + 4) % 4;
}
}
return (x == 0 && y == 0) || d != 0;
}
}
```
C++ 代码:
```C++
class Solution {
public:
bool isRobotBounded(string s) {
int x = 0, y = 0, d = 0;
int dirs[4][2] = {{0, 1}, {-1, 0}, {0, -1}, {1, 0}};
for (char c : s) {
if (c == 'G') {
x += dirs[d][0]; y += dirs[d][1];
} else if (c == 'L') {
d = (d + 1) % 4;
} else {
d = ((d - 1) % 4 + 4) % 4;
}
}
return (x == 0 && y == 0) || d != 0;
}
};
```
Python 代码:
```Python
class Solution:
def isRobotBounded(self, s: str) -> bool:
x, y, d = 0, 0, 0
dirs = [[0, 1], [-1, 0], [0, -1], [1, 0]]
for c in s:
if c == 'G':
x += dirs[d][0]
y += dirs[d][1]
elif c == 'L':
d = (d + 1) % 4
else:
d = ((d - 1) % 4 + 4) % 4
return (x == 0 and y == 0) or d != 0
```
Go 代码:
```Go
func isRobotBounded(s string) bool {
x, y, d := 0, 0, 0
dirs := [][]int{{0, 1}, {-1, 0}, {0, -1}, {1, 0}}
for _, c := range s {
if c == 'G' {
x += dirs[d][0]
y += dirs[d][1]
} else if c == 'L' {
d = (d + 1) % 4
} else {
d = ((d - 1) % 4 + 4) % 4
}
}
return (x == 0 && y == 0) || d != 0
}
```
TypeScript 代码:
```TypeScript
function isRobotBounded(s: string): boolean {
let x = 0, y = 0, d = 0;
const dirs: number[][] = [[0, 1], [-1, 0], [0, -1], [1, 0]];
for (const c of s) {
if (c === 'G') {
x += dirs[d][0];
y += dirs[d][1];
} else if (c === 'L') {
d = (d + 1) % 4;
} else {
d = ((d - 1) % 4 + 4) % 4;
}
}
return (x === 0 && y === 0) || d !== 0;
};
```
* 时间复杂度:$O(n)$
* 空间复杂度:$O(1)$

---

### 最后

这是我们「刷穿 LeetCode」系列文章的第 `No.1041` 篇,系列开始于 2021年01月01日,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。

在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。

为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode 。

在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。

Loading

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