问题描述
Initially, there is a Robot at position (0, 0). Given a sequence of its moves, judge if this robot makes a circle, which means it moves back to the original place.
The move sequence is represented by a string. And each move is represent by a character. The valid robot moves are R
(Right), L
(Left), U
(Up) and D
(down). The output should be true or false representing whether the robot makes a circle.
Example 1:
1 | Input: "UD" |
Example 2:
1 | Input: "LL" |
Related Topics: String
中文翻译版: 657. 判断路线成圈
解决方案
根据题目描述,可以设定一个二维坐标 (x, y),根据指令做相应的改变:
- Right: x = x + 1
- Left: x = x - 1
- Up: y = y + 1
- Down: y = y - 1
最后判断二维坐标 (x, y) 是否为原点 (0, 0) 即可。实现代码如下:
1 | #include <iostream> |