Judge Route Circle 机器人是否返回原点
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:
Input: “UD”
Output: true
Explanation: The robot moves up once, and then down once. All moves have the same magnitude, so it ended up at the origin where it started. Therefore, we return true.
Example 2:
Input: “LL”
Output: false
Explanation: The robot moves left twice. It ends up two “moves” to the left of the origin. We return false because it is not at the origin at the end of its moves.
Resolution
1 | //C++ |
Time Complexity: $O(N)$
Space Complexity: $O(1)$
learning note:
In C++, there are three ways to initialize variables. They are all equivalent and are reminiscent of the evolution of the language over the years:
First one, C-like Initialization
1
int x = 0;
type identifier (initial_value)
1
int x(0);
this was introduced by the revision of the C++ standard, in 2011
1
int x{0};
1 | //Java |