|
| 1 | +--- |
| 2 | +title: 657.机器人能否返回原点:模拟 |
| 3 | +date: 2026-04-05 13:27:28 |
| 4 | +tags: [题解, LeetCode, 简单, 字符串, 模拟] |
| 5 | +categories: [题解, LeetCode] |
| 6 | +--- |
| 7 | + |
| 8 | +# 【LetMeFly】657.机器人能否返回原点:模拟 |
| 9 | + |
| 10 | +力扣题目链接:[https://leetcode.cn/problems/robot-return-to-origin/](https://leetcode.cn/problems/robot-return-to-origin/) |
| 11 | + |
| 12 | +<p>在二维平面上,有一个机器人从原点 <code>(0, 0)</code> 开始。给出它的移动顺序,判断这个机器人在完成移动后是否在<strong> <code>(0, 0)</code> 处结束</strong>。</p> |
| 13 | + |
| 14 | +<p>移动顺序由字符串 <code>moves</code> 表示。字符 <code>move[i]</code> 表示其第 <code>i</code> 次移动。机器人的有效动作有 <code>R</code>(右),<code>L</code>(左),<code>U</code>(上)和 <code>D</code>(下)。</p> |
| 15 | + |
| 16 | +<p>如果机器人在完成所有动作后返回原点,则返回 <code>true</code>。否则,返回 <code>false</code>。</p> |
| 17 | + |
| 18 | +<p><strong>注意:</strong>机器人“面朝”的方向无关紧要。 <code>“R”</code> 将始终使机器人向右移动一次,<code>“L”</code> 将始终向左移动等。此外,假设每次移动机器人的移动幅度相同。</p> |
| 19 | + |
| 20 | +<p> </p> |
| 21 | + |
| 22 | +<p><strong>示例 1:</strong></p> |
| 23 | + |
| 24 | +<pre> |
| 25 | +<strong>输入:</strong> moves = "UD" |
| 26 | +<strong>输出:</strong> true |
| 27 | +<strong>解释:</strong>机器人向上移动一次,然后向下移动一次。所有动作都具有相同的幅度,因此它最终回到它开始的原点。因此,我们返回 true。</pre> |
| 28 | + |
| 29 | +<p><strong>示例 2:</strong></p> |
| 30 | + |
| 31 | +<pre> |
| 32 | +<strong>输入:</strong> moves = "LL" |
| 33 | +<strong>输出:</strong> false |
| 34 | +<strong>解释:</strong>机器人向左移动两次。它最终位于原点的左侧,距原点有两次 “移动” 的距离。我们返回 false,因为它在移动结束时没有返回原点。</pre> |
| 35 | + |
| 36 | +<p> </p> |
| 37 | + |
| 38 | +<p><strong>提示:</strong></p> |
| 39 | + |
| 40 | +<ul> |
| 41 | + <li><code>1 <= moves.length <= 2 * 10<sup>4</sup></code></li> |
| 42 | + <li><code>moves</code> 只包含字符 <code>'U'</code>, <code>'D'</code>, <code>'L'</code> 和 <code>'R'</code></li> |
| 43 | +</ul> |
| 44 | + |
| 45 | + |
| 46 | + |
| 47 | +## 解题方法:模拟 |
| 48 | + |
| 49 | +假设机器人处在x轴正方向水平向右y轴正方向水平向上的坐标系的原点: |
| 50 | + |
| 51 | ++ 每次`U`: `y++` |
| 52 | ++ 每次`L`: `x--` |
| 53 | ++ 每次`R`: `x++` |
| 54 | ++ 每次`D`: `y--` |
| 55 | + |
| 56 | +最终看机器人是否处在`(0, 0)`就好了。 |
| 57 | + |
| 58 | ++ 时间复杂度$O(len(moves))$ |
| 59 | ++ 空间复杂度$O(1)$ |
| 60 | + |
| 61 | +### AC代码 |
| 62 | + |
| 63 | +#### C++ |
| 64 | + |
| 65 | +```cpp |
| 66 | +/* |
| 67 | + * @LastEditTime: 2026-04-05 13:26:08 |
| 68 | + */ |
| 69 | +class Solution { |
| 70 | +public: |
| 71 | + bool judgeCircle(string moves) { |
| 72 | + int x = 0, y = 0; |
| 73 | + for (char c : moves) { |
| 74 | + switch (c) { |
| 75 | + case 'U': |
| 76 | + y++; |
| 77 | + break; |
| 78 | + |
| 79 | + case 'D': |
| 80 | + y--; |
| 81 | + break; |
| 82 | + |
| 83 | + case 'L': |
| 84 | + x--; |
| 85 | + break; |
| 86 | + |
| 87 | + case 'R': |
| 88 | + x++; |
| 89 | + break; |
| 90 | + |
| 91 | + default: |
| 92 | + break; |
| 93 | + } |
| 94 | + } |
| 95 | + return !x && !y; |
| 96 | + } |
| 97 | +}; |
| 98 | +``` |
| 99 | + |
| 100 | +> 同步发文于[CSDN](https://letmefly.blog.csdn.net/article/details/159855671)和我的[个人博客](https://blog.letmefly.xyz/),原创不易,转载经作者同意后请附上[原文链接](https://blog.letmefly.xyz/2026/04/05/LeetCode%200657.%E6%9C%BA%E5%99%A8%E4%BA%BA%E8%83%BD%E5%90%A6%E8%BF%94%E5%9B%9E%E5%8E%9F%E7%82%B9/)哦~ |
| 101 | +> |
| 102 | +> 千篇源码题解[已开源](https://github.com/LetMeFly666/LeetCode) |
0 commit comments