-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem1041.java
More file actions
35 lines (35 loc) · 1 KB
/
problem1041.java
File metadata and controls
35 lines (35 loc) · 1 KB
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
class Solution {
public boolean isRobotBounded(String instructions) {
int x = 0;
int y = 0;
String[] move = {"U", "R", "D", "L"};
int pos = 0;
for(int i = 0; i < instructions.length(); i++){
if(instructions.charAt(i) == 'L'){
if(pos == 0){
pos = 3;
}else{
pos -= 1;
}
}else if(instructions.charAt(i) == 'R'){
if(pos == 3){
pos = 0;
}else{
pos += 1;
}
}else{
switch(move[pos]){
case "U": y++;
break;
case "D": y--;
break;
case "R": x++;
break;
case "L": x--;
break;
}
}
}
return (!(move[pos].equals("U")) || (x == 0 && y == 0));
}
}