-
Notifications
You must be signed in to change notification settings - Fork 31
/
2120.java
36 lines (35 loc) · 1.07 KB
/
2120.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 int[] executeInstructions(int n, int[] startPos, String s) {
int[] res = new int[s.length()];
int k = 0;
for(int i=0;i<s.length();i++){
int x = startPos[0], y = startPos[1], counter = 0;
for(int j=i;j<s.length();j++){
if(s.charAt(j) == 'R'){
if(y+1 == n){
break;
}
y++;
}else if(s.charAt(j) == 'L'){
if(y-1 == -1){
break;
}
y--;
}else if(s.charAt(j) == 'U'){
if(x-1 == -1){
break;
}
x--;
}else if(s.charAt(j) == 'D'){
if(x+1 == n){
break;
}
x++;
}
counter++;
}
res[k++] = counter;
}
return res;
}
}