Skip to content

Commit 7011810

Browse files
committed
一刷1041
1 parent 5c7ab49 commit 7011810

File tree

6 files changed

+84
-9
lines changed

6 files changed

+84
-9
lines changed

README.adoc

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7312,14 +7312,14 @@ TIP: **公众号的微信号是: `jikerizhi`**。__因为众所周知的原因
73127312
//|{doc_base_url}/1040-moving-stones-until-consecutive-ii.adoc[题解]
73137313
//|Medium
73147314
//|
7315-
//
7316-
//|{counter:codes}
7317-
//|{leetcode_base_url}/robot-bounded-in-circle/[1041. Robot Bounded In Circle^]
7318-
//|{source_base_url}/_1041_RobotBoundedInCircle.java[Java]
7319-
//|{doc_base_url}/1041-robot-bounded-in-circle.adoc[题解]
7320-
//|Medium
7321-
//|
7322-
//
7315+
7316+
|{counter:codes}
7317+
|{leetcode_base_url}/robot-bounded-in-circle/[1041. Robot Bounded In Circle^]
7318+
|{source_base_url}/_1041_RobotBoundedInCircle.java[Java]
7319+
|{doc_base_url}/1041-robot-bounded-in-circle.adoc[题解]
7320+
|Medium
7321+
|
7322+
73237323
//|{counter:codes}
73247324
//|{leetcode_base_url}/flower-planting-with-no-adjacent/[1042. Flower Planting With No Adjacent^]
73257325
//|{source_base_url}/_1042_FlowerPlantingWithNoAdjacent.java[Java]

docs/0000-00-note.adoc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ image::images/quick-sort-01.gif[{image_attr}]
2626
* [ ] 买卖股票
2727
* [ ] 链表复制相关操作,也可以将新旧节点交替相连来处理。
2828

29+
== 感慨
30+
31+
问题也懂,代码也可以写出来,但是写的不够简洁优美,怎么办?
32+
2933
== 解题范式
3034

3135
. xref:0000-06-two-pointer.adoc[Two Pointer 双指针]

docs/1041-robot-bounded-in-circle.adoc

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,42 @@ The robot moves from (0, 0) -> (0, 1) -> (-1, 1) -> (-1, 0) -> (0, 0) -> ...
5656
* `1 <= instructions.length <= 100`
5757
* `instructions[i]` is in `{'G', 'L', 'R'}`
5858

59+
== 思路分析
5960

61+
TIP: 这道题不是要看路径是否相交。
6062

63+
这道题的关键点事找出不存在环路的条件:执行一遍指令后,机器人必须不在原点且方向继续朝北:
64+
65+
* 如果在原点,无论执行多少遍,结果都会在原点。
66+
* 如果方向不是初始化方向,那么多次执行后,就会相互抵消,形成环路。
6167

6268
[[src-1041]]
69+
[tabs]
70+
====
71+
一刷::
72+
+
73+
--
6374
[{java_src_attr}]
6475
----
6576
include::{sourcedir}/_1041_RobotBoundedInCircle.java[tag=answer]
6677
----
78+
--
79+
80+
// 二刷::
81+
// +
82+
// --
83+
// [{java_src_attr}]
84+
// ----
85+
// include::{sourcedir}/_1041_RobotBoundedInCircle_2.java[tag=answer]
86+
// ----
87+
// --
88+
====
89+
90+
== 参考资料
91+
92+
. https://leetcode.cn/problems/robot-bounded-in-circle/solutions/2217873/kun-yu-huan-zhong-de-ji-qi-ren-by-leetco-kjya/[1041. 困于环中的机器人 - 官方题解^]
93+
. https://leetcode.cn/problems/robot-bounded-in-circle/solutions/2217873/kun-yu-huan-zhong-de-ji-qi-ren-by-leetco-kjya/comments/1985848[1041. 困于环中的机器人 / 矩阵变换的思路^]
94+
. https://leetcode.cn/problems/robot-bounded-in-circle/solutions/2219798/python3javacgotypescript-yi-ti-yi-jie-mo-cyda/[1041. 困于环中的机器人 - 一题一解:模拟(详细题解)^]
95+
. https://leetcode.cn/problems/robot-bounded-in-circle/solutions/2347537/gong-shui-san-xie-chang-gui-mo-ni-ti-by-hgdtp/[1041. 困于环中的机器人 - 【宫水三叶】常规模拟题^]
96+
6797

docs/index.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2156,7 +2156,7 @@ include::1011-capacity-to-ship-packages-within-d-days.adoc[leveloffset=+1]
21562156

21572157
// include::1040-moving-stones-until-consecutive-ii.adoc[leveloffset=+1]
21582158

2159-
// include::1041-robot-bounded-in-circle.adoc[leveloffset=+1]
2159+
include::1041-robot-bounded-in-circle.adoc[leveloffset=+1]
21602160

21612161
// include::1042-flower-planting-with-no-adjacent.adoc[leveloffset=+1]
21622162

logbook/202406.adoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -898,6 +898,11 @@
898898
|{doc_base_url}/0904-fruit-into-baskets.adoc[题解]
899899
|✅ 滑动窗口
900900

901+
|{counter:codes}
902+
|{leetcode_base_url}/robot-bounded-in-circle/[1041. Robot Bounded In Circle^]
903+
|{source_base_url}/_1041_RobotBoundedInCircle.java[Java]
904+
|⭕️ 理解题目,确定符合要求的条件。
905+
901906
902907
|===
903908
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.diguage.algo.leetcode;
2+
3+
public class _1041_RobotBoundedInCircle {
4+
// tag::answer[]
5+
6+
/**
7+
* @author D瓜哥 · https://www.diguage.com
8+
* @since 2024-09-27 16:10:21
9+
*/
10+
public boolean isRobotBounded(String instructions) {
11+
int x = 0, y = 0;
12+
int dx = 0, dy = 1;
13+
for (int i = 0; i < instructions.length(); i++) {
14+
char step = instructions.charAt(i);
15+
if (step == 'G') {
16+
x += dx;
17+
y += dy;
18+
} else if (step == 'R') {
19+
int temp = dx;
20+
dx = dy;
21+
dy = -temp;
22+
} else if (step == 'L') {
23+
int temp = dx;
24+
dx = -dy;
25+
dy = temp;
26+
}
27+
}
28+
return !(dx == 0 && dy == 1) || (x == 0 && y == 0);
29+
}
30+
31+
// end::answer[]
32+
public static void main(String[] args) {
33+
new _1041_RobotBoundedInCircle()
34+
.isRobotBounded("GLGLGGLGL");
35+
}
36+
}

0 commit comments

Comments
 (0)