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.解题要点:
class Solution {
public boolean judgeCircle(String moves) {
if(moves.length() == 0) return true;
int[] d = {0, 0};
for(char c : moves.toCharArray()){
if(c == 'R') d[0]++;
else if(c == 'L') d[0]--;
else if(c == 'U') d[1]++;
else d[1]--;
}
return d[0] == 0 && d[1] == 0;
}
}Last updated