Skip to content

Commit 6d0ed2b

Browse files
Bruce YangBruce Yang
authored andcommitted
Add day32 post.
1 parent c2adc39 commit 6d0ed2b

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed

91algo/daily/posts/day32.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
break;
42+
case 'R':
43+
x++;
44+
break;
45+
case 'L':
46+
x--;
47+
break;
48+
}
49+
}
50+
return x == 0 && y == 0;
51+
}
52+
};
53+
```
54+
55+
56+
## 复杂度分析
57+
58+
- 时间复杂度:O(n)
59+
- 空间复杂度:O(1)

0 commit comments

Comments
 (0)