Skip to content

Commit

Permalink
一刷1041
Browse files Browse the repository at this point in the history
  • Loading branch information
diguage committed Sep 27, 2024
1 parent 5c7ab49 commit 7011810
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 9 deletions.
16 changes: 8 additions & 8 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -7312,14 +7312,14 @@ TIP: **公众号的微信号是: `jikerizhi`**。__因为众所周知的原因
//|{doc_base_url}/1040-moving-stones-until-consecutive-ii.adoc[题解]
//|Medium
//|
//
//|{counter:codes}
//|{leetcode_base_url}/robot-bounded-in-circle/[1041. Robot Bounded In Circle^]
//|{source_base_url}/_1041_RobotBoundedInCircle.java[Java]
//|{doc_base_url}/1041-robot-bounded-in-circle.adoc[题解]
//|Medium
//|
//

|{counter:codes}
|{leetcode_base_url}/robot-bounded-in-circle/[1041. Robot Bounded In Circle^]
|{source_base_url}/_1041_RobotBoundedInCircle.java[Java]
|{doc_base_url}/1041-robot-bounded-in-circle.adoc[题解]
|Medium
|

//|{counter:codes}
//|{leetcode_base_url}/flower-planting-with-no-adjacent/[1042. Flower Planting With No Adjacent^]
//|{source_base_url}/_1042_FlowerPlantingWithNoAdjacent.java[Java]
Expand Down
4 changes: 4 additions & 0 deletions docs/0000-00-note.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ image::images/quick-sort-01.gif[{image_attr}]
* [ ] 买卖股票
* [ ] 链表复制相关操作,也可以将新旧节点交替相连来处理。

== 感慨

问题也懂,代码也可以写出来,但是写的不够简洁优美,怎么办?

== 解题范式

. xref:0000-06-two-pointer.adoc[Two Pointer 双指针]
Expand Down
30 changes: 30 additions & 0 deletions docs/1041-robot-bounded-in-circle.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,42 @@ The robot moves from (0, 0) -> (0, 1) -> (-1, 1) -> (-1, 0) -> (0, 0) -> ...
* `1 <= instructions.length <= 100`
* `instructions[i]` is in `{'G', 'L', 'R'}`

== 思路分析

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

这道题的关键点事找出不存在环路的条件:执行一遍指令后,机器人必须不在原点且方向继续朝北:

* 如果在原点,无论执行多少遍,结果都会在原点。
* 如果方向不是初始化方向,那么多次执行后,就会相互抵消,形成环路。

[[src-1041]]
[tabs]
====
一刷::
+
--
[{java_src_attr}]
----
include::{sourcedir}/_1041_RobotBoundedInCircle.java[tag=answer]
----
--
// 二刷::
// +
// --
// [{java_src_attr}]
// ----
// include::{sourcedir}/_1041_RobotBoundedInCircle_2.java[tag=answer]
// ----
// --
====

== 参考资料

. https://leetcode.cn/problems/robot-bounded-in-circle/solutions/2217873/kun-yu-huan-zhong-de-ji-qi-ren-by-leetco-kjya/[1041. 困于环中的机器人 - 官方题解^]
. https://leetcode.cn/problems/robot-bounded-in-circle/solutions/2217873/kun-yu-huan-zhong-de-ji-qi-ren-by-leetco-kjya/comments/1985848[1041. 困于环中的机器人 / 矩阵变换的思路^]
. https://leetcode.cn/problems/robot-bounded-in-circle/solutions/2219798/python3javacgotypescript-yi-ti-yi-jie-mo-cyda/[1041. 困于环中的机器人 - 一题一解:模拟(详细题解)^]
. https://leetcode.cn/problems/robot-bounded-in-circle/solutions/2347537/gong-shui-san-xie-chang-gui-mo-ni-ti-by-hgdtp/[1041. 困于环中的机器人 - 【宫水三叶】常规模拟题^]


2 changes: 1 addition & 1 deletion docs/index.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -2156,7 +2156,7 @@ include::1011-capacity-to-ship-packages-within-d-days.adoc[leveloffset=+1]

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

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

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

Expand Down
5 changes: 5 additions & 0 deletions logbook/202406.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -898,6 +898,11 @@
|{doc_base_url}/0904-fruit-into-baskets.adoc[题解]
|✅ 滑动窗口

|{counter:codes}
|{leetcode_base_url}/robot-bounded-in-circle/[1041. Robot Bounded In Circle^]
|{source_base_url}/_1041_RobotBoundedInCircle.java[Java]
|⭕️ 理解题目,确定符合要求的条件。
|===
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.diguage.algo.leetcode;

public class _1041_RobotBoundedInCircle {
// tag::answer[]

/**
* @author D瓜哥 · https://www.diguage.com
* @since 2024-09-27 16:10:21
*/
public boolean isRobotBounded(String instructions) {
int x = 0, y = 0;
int dx = 0, dy = 1;
for (int i = 0; i < instructions.length(); i++) {
char step = instructions.charAt(i);
if (step == 'G') {
x += dx;
y += dy;
} else if (step == 'R') {
int temp = dx;
dx = dy;
dy = -temp;
} else if (step == 'L') {
int temp = dx;
dx = -dy;
dy = temp;
}
}
return !(dx == 0 && dy == 1) || (x == 0 && y == 0);
}

// end::answer[]
public static void main(String[] args) {
new _1041_RobotBoundedInCircle()
.isRobotBounded("GLGLGGLGL");
}
}

0 comments on commit 7011810

Please sign in to comment.