- Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy path_1138.java
73 lines (69 loc) · 2.34 KB
/
_1138.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
packagecom.fishercoder.solutions.secondthousand;
importjava.util.HashMap;
importjava.util.Map;
publicclass_1138 {
publicstaticclassSolution1 {
publicStringalphabetBoardPath(Stringtarget) {
Map<Character, int[]> map = initMap();
StringBuildersb = newStringBuilder();
int[] currPos = newint[2];
for (charc : target.toCharArray()) {
moveToDest(currPos, map.get(c), sb);
}
returnsb.toString();
}
privatevoidmoveToDest(int[] currPos, int[] dest, StringBuildersb) {
intcurrRow = currPos[0];
intcurrCol = currPos[1];
intdestRow = dest[0];
intdestCol = dest[1];
while (currRow != destRow || currCol != destCol) {
if (currRow < destRow) {
while (currRow < destRow) {
if (currCol != 0 && currRow == 4) {
break;
}
sb.append("D");
currRow++;
}
currPos[0] = currRow;
}
if (currRow > destRow) {
while (currRow > destRow) {
sb.append("U");
currRow--;
}
currPos[0] = currRow;
}
if (currCol < destCol) {
while (currCol < destCol) {
sb.append("R");
currCol++;
}
currPos[1] = currCol;
}
if (currCol > destCol) {
while (currCol > destCol) {
sb.append("L");
currCol--;
}
currPos[1] = currCol;
}
}
sb.append("!");
return;
}
privateMap<Character, int[]> initMap() {
Map<Character, int[]> map = newHashMap<>();
introw;
intcol;
intnumber = 0;
for (charc = 'a'; c <= 'z'; c++, number++) {
row = number / 5;
col = number % 5;
map.put(c, newint[] {row, col});
}
returnmap;
}
}
}