mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-01-11 02:58:13 +08:00
76 lines
3.5 KiB
HTML
76 lines
3.5 KiB
HTML
|
<p>On an infinite plane, a robot initially stands at <code>(0, 0)</code> and faces north. Note that:</p>
|
||
|
|
||
|
<ul>
|
||
|
<li>The <strong>north direction</strong> is the positive direction of the y-axis.</li>
|
||
|
<li>The <strong>south direction</strong> is the negative direction of the y-axis.</li>
|
||
|
<li>The <strong>east direction</strong> is the positive direction of the x-axis.</li>
|
||
|
<li>The <strong>west direction</strong> is the negative direction of the x-axis.</li>
|
||
|
</ul>
|
||
|
|
||
|
<p>The robot can receive one of three instructions:</p>
|
||
|
|
||
|
<ul>
|
||
|
<li><code>"G"</code>: go straight 1 unit.</li>
|
||
|
<li><code>"L"</code>: turn 90 degrees to the left (i.e., anti-clockwise direction).</li>
|
||
|
<li><code>"R"</code>: turn 90 degrees to the right (i.e., clockwise direction).</li>
|
||
|
</ul>
|
||
|
|
||
|
<p>The robot performs the <code>instructions</code> given in order, and repeats them forever.</p>
|
||
|
|
||
|
<p>Return <code>true</code> if and only if there exists a circle in the plane such that the robot never leaves the circle.</p>
|
||
|
|
||
|
<p> </p>
|
||
|
<p><strong>Example 1:</strong></p>
|
||
|
|
||
|
<pre>
|
||
|
<strong>Input:</strong> instructions = "GGLLGG"
|
||
|
<strong>Output:</strong> true
|
||
|
<strong>Explanation:</strong> The robot is initially at (0, 0) facing the north direction.
|
||
|
"G": move one step. Position: (0, 1). Direction: North.
|
||
|
"G": move one step. Position: (0, 2). Direction: North.
|
||
|
"L": turn 90 degrees anti-clockwise. Position: (0, 2). Direction: West.
|
||
|
"L": turn 90 degrees anti-clockwise. Position: (0, 2). Direction: South.
|
||
|
"G": move one step. Position: (0, 1). Direction: South.
|
||
|
"G": move one step. Position: (0, 0). Direction: South.
|
||
|
Repeating the instructions, the robot goes into the cycle: (0, 0) --> (0, 1) --> (0, 2) --> (0, 1) --> (0, 0).
|
||
|
Based on that, we return true.
|
||
|
</pre>
|
||
|
|
||
|
<p><strong>Example 2:</strong></p>
|
||
|
|
||
|
<pre>
|
||
|
<strong>Input:</strong> instructions = "GG"
|
||
|
<strong>Output:</strong> false
|
||
|
<strong>Explanation:</strong> The robot is initially at (0, 0) facing the north direction.
|
||
|
"G": move one step. Position: (0, 1). Direction: North.
|
||
|
"G": move one step. Position: (0, 2). Direction: North.
|
||
|
Repeating the instructions, keeps advancing in the north direction and does not go into cycles.
|
||
|
Based on that, we return false.
|
||
|
</pre>
|
||
|
|
||
|
<p><strong>Example 3:</strong></p>
|
||
|
|
||
|
<pre>
|
||
|
<strong>Input:</strong> instructions = "GL"
|
||
|
<strong>Output:</strong> true
|
||
|
<strong>Explanation:</strong> The robot is initially at (0, 0) facing the north direction.
|
||
|
"G": move one step. Position: (0, 1). Direction: North.
|
||
|
"L": turn 90 degrees anti-clockwise. Position: (0, 1). Direction: West.
|
||
|
"G": move one step. Position: (-1, 1). Direction: West.
|
||
|
"L": turn 90 degrees anti-clockwise. Position: (-1, 1). Direction: South.
|
||
|
"G": move one step. Position: (-1, 0). Direction: South.
|
||
|
"L": turn 90 degrees anti-clockwise. Position: (-1, 0). Direction: East.
|
||
|
"G": move one step. Position: (0, 0). Direction: East.
|
||
|
"L": turn 90 degrees anti-clockwise. Position: (0, 0). Direction: North.
|
||
|
Repeating the instructions, the robot goes into the cycle: (0, 0) --> (0, 1) --> (-1, 1) --> (-1, 0) --> (0, 0).
|
||
|
Based on that, we return true.
|
||
|
</pre>
|
||
|
|
||
|
<p> </p>
|
||
|
<p><strong>Constraints:</strong></p>
|
||
|
|
||
|
<ul>
|
||
|
<li><code>1 <= instructions.length <= 100</code></li>
|
||
|
<li><code>instructions[i]</code> is <code>'G'</code>, <code>'L'</code> or, <code>'R'</code>.</li>
|
||
|
</ul>
|