forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_2120.java
54 lines (53 loc) · 1.82 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package com.fishercoder.solutions;
public class _2120 {
public static class Solution1 {
public int[] executeInstructions(int n, int[] startPos, String s) {
int[] ans = new int[s.length()];
for (int i = 0; i < s.length(); i++) {
int y = startPos[1];
int x = startPos[0];
int j = i;
boolean broken = false;
for (; j < s.length(); j++) {
if (s.charAt(j) == 'R') {
if (y + 1 < n) {
y++;
} else {
ans[i] = j - i;
broken = true;
break;
}
} else if (s.charAt(j) == 'L') {
if (y - 1 >= 0) {
y--;
} else {
ans[i] = j - i;
broken = true;
break;
}
} else if (s.charAt(j) == 'U') {
if (x - 1 >= 0) {
x--;
} else {
ans[i] = j - i;
broken = true;
break;
}
} else if (s.charAt(j) == 'D') {
if (x + 1 < n) {
x++;
} else {
ans[i] = j - i;
broken = true;
break;
}
}
}
if (!broken) {
ans[i] = j - i;
}
}
return ans;
}
}
}