We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent c2adc39 commit 6d0ed2bCopy full SHA for 6d0ed2b
91algo/daily/posts/day32.md
@@ -0,0 +1,59 @@
1
+# day32 - leetcode657. 机器人能否返回原点[2021-10-11]
2
+
3
+## 题目地址(657. 机器人能否返回原点)
4
5
+<https://leetcode-cn.com/problems/robot-return-to-origin/>
6
7
8
+## 思路
9
10
+### 模拟法
11
12
13
14
+定义一个初始坐标(x, y), 每次从moves数组读入一个字符, 根据具体反向进行一次x或y的增减。
15
+看循环结束时, 是否能使 x == 0 且 y ==0 。
16
17
18
19
+## 代码
20
21
22
+### 实现语言: C++
23
24
25
26
+```cpp
27
+class Solution {
28
+public:
29
+ bool judgeCircle(string moves) {
30
+ int x = 0;
31
+ int y = 0;
32
+ for (const auto& move : moves)
33
+ {
34
+ switch (move)
35
36
+ case 'U':
37
+ y--;
38
+ break;
39
+ case 'D':
40
+ y++;
41
42
+ case 'R':
43
+ x++;
44
45
+ case 'L':
46
+ x--;
47
48
+ }
49
50
+ return x == 0 && y == 0;
51
52
+};
53
+```
54
55
56
+## 复杂度分析
57
58
+- 时间复杂度:O(n)
59
+- 空间复杂度:O(1)
0 commit comments