Skip to content

Commit fb6f67b

Browse files
committed
Runtime: 2 ms (Top 16.85%) | Memory: 41.9 MB (Top 53.99%)
1 parent 04ebaac commit fb6f67b

File tree

1 file changed

+28
-27
lines changed

1 file changed

+28
-27
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,28 @@
1+
// Runtime: 2 ms (Top 16.85%) | Memory: 41.9 MB (Top 53.99%)
12
class Solution {
23
public boolean isRobotBounded(String instructions) {
34
if (instructions.length() == 0) {
45
return true;
56
}
6-
7+
78
Robot bender = new Robot();
89
int[] start = new int[]{0, 0};
9-
10-
// 4 represents the max 90 degree turns that can restart initial orientation.
10+
11+
// 4 represents the max 90 degree turns that can restart initial orientation.
1112
for (int i = 0; i < 4; i++) {
1213
boolean orientationChanged = bender.performSet(instructions);
13-
14+
1415
int[] location = bender.location;
1516
if (location[0] == start[0] && location[1] == start[1]) {
1617
return true;
17-
}
18-
19-
// If robot never turns and the first instruction isn't at start, exit.
20-
else if (!orientationChanged) {
18+
}
19+
20+
// If robot never turns and the first instruction isn't at start, exit.
21+
else if (!orientationChanged) {
2122
return false;
2223
}
2324
}
24-
25+
2526
return false;
2627
}
2728
}
@@ -32,56 +33,56 @@ class Robot {
3233
int[][] orientations;
3334
int orientationPos;
3435
boolean orientationChangeCheck;
35-
36+
3637
Robot() {
37-
// Start in center
38+
// Start in center
3839
location = new int[]{0, 0};
39-
40-
// N, E, S, W
41-
orientations = new int[][]{{1,0}, {0, 1}, {-1, 0}, {0, -1}};
42-
43-
// Start pointing north
40+
41+
// N, E, S, W
42+
orientations = new int[][]{{1,0}, {0, 1}, {-1, 0}, {0, -1}};
43+
44+
// Start pointing north
4445
orientationPos = 0;
4546
orientation = orientations[orientationPos];
46-
47-
// Track if robot has turned
47+
48+
// Track if robot has turned
4849
orientationChangeCheck = false;
4950
}
50-
51+
5152
public boolean performSet(String orders) {
5253
this.orientationChangeCheck = false;
53-
54+
5455
for (int i = 0; i < orders.length(); i++) {
5556
this.perform(orders.charAt(i));
5657
}
57-
58+
5859
return this.orientationChangeCheck;
5960
}
60-
61+
6162
public void perform(char order) {
6263
if (order == 'G') {
6364
this.go();
6465
} else if (order == 'L' || order == 'R') {
6566
this.turn(order);
6667
} else {
67-
// do nothing
68+
// do nothing
6869
}
6970
}
70-
71+
7172
public void turn(char direction) {
7273
if (direction == 'L') {
7374
this.orientationPos = this.orientationPos == 0 ? 3 : this.orientationPos - 1;
7475
} else if (direction == 'R') {
7576
this.orientationPos = (this.orientationPos + 1) % 4;
7677
}
77-
78+
7879
this.orientation = this.orientations[this.orientationPos];
7980
this.orientationChangeCheck = true;
8081
}
81-
82+
8283
public int[] go() {
8384
this.location[0] += this.orientation[0];
8485
this.location[1] += this.orientation[1];
8586
return this.location;
8687
}
87-
}
88+
}

0 commit comments

Comments
 (0)