-
Notifications
You must be signed in to change notification settings - Fork 0
/
RobotBoundedInCircle.java
36 lines (35 loc) · 1.41 KB
/
RobotBoundedInCircle.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
class Solution {
public boolean isRobotBounded(String instructions) {
int[] navigation = navigate(instructions, 0, 0, 'N');
return ((navigation[0] == 0 && navigation[1] == 0) || navigation[2] != 'N');
}
public static int[] navigate(String instructions, int currentX, int currentY, char currentDirection) {
int n = instructions.length();
for(int i = 0; i < n; i++) {
char currentInst = instructions.charAt(i);
if(currentInst == 'G') {
if(currentDirection == 'N')
currentX++;
else if(currentDirection == 'S')
currentX--;
else if(currentDirection == 'W')
currentY--;
else if(currentDirection == 'E')
currentY++;
} else
currentDirection = turn(currentDirection, currentInst);
}
return new int[] {currentX, currentY, (int) currentDirection};
}
public static char turn(char currentDirection, char inst) {
if(currentDirection == 'N')
return (inst == 'L') ? 'W' : 'E';
else if(currentDirection == 'S')
return (inst == 'L') ? 'E' : 'W';
else if(currentDirection == 'W')
return (inst == 'L') ? 'S' : 'N';
else if(currentDirection == 'E')
return (inst == 'L') ? 'N' : 'S';
return ' ';
}
}