题目
题目链接
题解
class Solution {
public boolean isRobotBounded(String instructions) {
if (instructions == null || "".equals(instructions)) {
return true;
}
return isRound(instructions);
}
private boolean isRound(String command) {
// 0北 1东 2南 3西
int face = 0;
int x = 0;
int y = 0;
for (int i = 0; i < command.length(); i++) {
char ins = command.charAt(i);
if (ins == 'G') {
if (face == 0) {
y++;
} else if (face == 1) {
x++;
} else if (face == 2) {
y--;
} else if (face == 3) {
x--;
}
} else if (ins == 'L') {
face += 3;
face %= 4;
} else if (ins == 'R') {
face++;
face %= 4;
}
}
return x == 0 && y == 0 || face != 0;
}
}