Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitb1d9899

Browse files
add 1138
1 parent424e885 commitb1d9899

File tree

3 files changed

+101
-0
lines changed

3 files changed

+101
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@ _If you like this project, please leave me a star._ ★
223223
|1154|[Day of the Year](https://leetcode.com/problems/day-of-the-year/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1154.java)||Easy||
224224
|1150|[Check If a Number Is Majority Element in a Sorted Array](https://leetcode.com/problems/check-if-a-number-is-majority-element-in-a-sorted-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1150.java)|[:tv:](https://youtu.be/-t2cdVs9cKk)|Easy||
225225
|1146|[Snapshot Array](https://leetcode.com/problems/snapshot-array/)|[Javascript](./javascript/_1146.js)|| Easy||
226+
|1138|[Alphabet Board Path](https://leetcode.com/problems/alphabet-board-path/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1138.java)|| Medium|HashTable, String|
226227
|1137|[N-th Tribonacci Number](https://leetcode.com/problems/n-th-tribonacci-number/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1137.java)||Easy||
227228
|1134|[Armstrong Number](https://leetcode.com/problems/armstrong-number/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1134.java)|[:tv:](https://www.youtube.com/watch?v=HTL7fd4HPf4)|Easy||
228229
|1133|[Largest Unique Number](https://leetcode.com/problems/largest-unique-number/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1133.java)|[:tv:](https://youtu.be/Fecpt1YZlCs)|Easy||
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
packagecom.fishercoder.solutions;
2+
3+
importjava.util.HashMap;
4+
importjava.util.Map;
5+
6+
publicclass_1138 {
7+
publicstaticclassSolution1 {
8+
publicStringalphabetBoardPath(Stringtarget) {
9+
Map<Character,int[]>map =initMap();
10+
StringBuildersb =newStringBuilder();
11+
int[]currPos =newint[2];
12+
for (charc :target.toCharArray()) {
13+
moveToDest(currPos,map.get(c),sb);
14+
}
15+
returnsb.toString();
16+
}
17+
18+
privatevoidmoveToDest(int[]currPos,int[]dest,StringBuildersb) {
19+
intcurrRow =currPos[0];
20+
intcurrCol =currPos[1];
21+
intdestRow =dest[0];
22+
intdestCol =dest[1];
23+
while (currRow !=destRow ||currCol !=destCol) {
24+
if (currRow <destRow) {
25+
while (currRow <destRow) {
26+
if (currCol !=0 &&currRow ==4) {
27+
break;
28+
}
29+
sb.append("D");
30+
currRow++;
31+
}
32+
currPos[0] =currRow;
33+
}
34+
if (currRow >destRow) {
35+
while (currRow >destRow) {
36+
sb.append("U");
37+
currRow--;
38+
}
39+
currPos[0] =currRow;
40+
}
41+
if (currCol <destCol) {
42+
while (currCol <destCol) {
43+
sb.append("R");
44+
currCol++;
45+
}
46+
currPos[1] =currCol;
47+
}
48+
if (currCol >destCol) {
49+
while (currCol >destCol) {
50+
sb.append("L");
51+
currCol--;
52+
}
53+
currPos[1] =currCol;
54+
}
55+
}
56+
sb.append("!");
57+
return;
58+
}
59+
60+
privateMap<Character,int[]>initMap() {
61+
Map<Character,int[]>map =newHashMap<>();
62+
introw;
63+
intcol;
64+
intnumber =0;
65+
for (charc ='a';c <='z';c++,number++) {
66+
row =number /5;
67+
col =number %5;
68+
map.put(c,newint[]{row,col});
69+
}
70+
returnmap;
71+
}
72+
}
73+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
packagecom.fishercoder;
2+
3+
importcom.fishercoder.solutions._1138;
4+
importorg.junit.BeforeClass;
5+
importorg.junit.Test;
6+
7+
importstaticorg.junit.Assert.assertEquals;
8+
9+
publicclass_1138Test {
10+
privatestatic_1138.Solution1solution1;
11+
12+
@BeforeClass
13+
publicstaticvoidsetup() {
14+
solution1 =new_1138.Solution1();
15+
}
16+
17+
@Test
18+
publicvoidtest1() {
19+
assertEquals("DDR!UURRR!!DDD!",solution1.alphabetBoardPath("leet"));
20+
}
21+
22+
@Test
23+
publicvoidtest2() {
24+
assertEquals("DDDDD!UUUUURRR!DDDDLLLD!",solution1.alphabetBoardPath("zdz"));
25+
}
26+
27+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp