657. Robot Return to Origin
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.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.bool judgeCircle(string moves) { // time: O(n); space: O(1)
int x = 0, y = 0;
for (char ch : moves) {
switch(ch) {
case 'R': ++x; break;
case 'L': --x; break;
case 'U': --y; break;
case 'D': ++y; break;
}
}
return x == 0 && y == 0;
}Last updated