-
Notifications
You must be signed in to change notification settings - Fork 0
/
1041. Robot Bounded In Circle.java
32 lines (28 loc) · 1.36 KB
/
1041. Robot Bounded In Circle.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
// 1041. Robot Bounded In Circle
class Solution {
public boolean isRobotBounded(String instructions) {
int[] directionVector = new int[] {0,1}; // directional vector
int[] startingVector = new int[] {0,0}; // starting vector
// G, add 1 to y axis -> starting Vector
// L, interchange x and y and make X minus -> direction vector
// R, interchange x and y and make Y minus -> direction vector
for (int i = 0; i < 4; i++) {
for (char instruction : instructions.toCharArray()) {
if ('G' == instruction) {
startingVector[0] += directionVector[0];
startingVector[1] += directionVector[1];
} else if ('L' == instruction) {
int temp = directionVector[0];
directionVector[0] = -1 * directionVector[1];
directionVector[1] = temp;
} else if ('R' == instruction) {
int temp = directionVector[1];
directionVector[1] = -1 * directionVector[0];
directionVector[0] = temp;
}
}
}
// first condition means it's back to starting point. Second means it's just going up up and upward.
return (startingVector[0] == 0 && startingVector[1] == 0) || (directionVector[0] != 0 && directionVector[1] != 1);
}
}