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
U (Up) and
D (down). The output should be true or false representing whether the robot makes a circle.
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.
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.
Time Complexity: $O(N)$
Space Complexity: $O(1)$
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
int x = 0;
type identifier (initial_value)
this was introduced by the revision of the C++ standard, in 2011