Skip to content

Commit 5a80c74

Browse files
authored
Merge pull request #1132 from 0xff-dev/1138
Add solution and test-cases for problem 1138
2 parents 26aabfe + a3d3397 commit 5a80c74

File tree

4 files changed

+94
-23
lines changed

4 files changed

+94
-23
lines changed
Loading

leetcode/1101-1200/1138.Alphabet-Board-Path/README.md

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,37 @@
11
# [1138.Alphabet Board Path][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
On an alphabet board, we start at position `(0, 0)`, corresponding to character `board[0][0]`.
5+
6+
Here, `board = ["abcde", "fghij", "klmno", "pqrst", "uvwxy", "z"]`, as shown in the diagram below.
7+
8+
![1](./1.png)
9+
10+
We may make the following moves:
11+
12+
- `'U'` moves our position up one row, if the position exists on the board;
13+
- `'D'` moves our position down one row, if the position exists on the board;
14+
- `'L'` moves our position left one column, if the position exists on the board;
15+
- `'R'` moves our position right one column, if the position exists on the board;
16+
- `'!'` adds the character `board[r][c]` at our current position `(r, c)` to the answer.
17+
18+
(Here, the only positions that exist on the board are positions with letters on them.)
19+
20+
Return a sequence of moves that makes our answer equal to `target` in the minimum number of moves. You may return any path that does so.
721

822
**Example 1:**
923

1024
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
25+
Input: target = "leet"
26+
Output: "DDR!UURRR!!DDD!"
1327
```
1428

15-
## 题意
16-
> ...
17-
18-
## 题解
29+
**Example 2:**
1930

20-
### 思路1
21-
> ...
22-
Alphabet Board Path
23-
```go
2431
```
25-
32+
Input: target = "code"
33+
Output: "RR!DDRR!UUL!R!"
34+
```
2635

2736
## 结语
2837

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,68 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
import "strings"
4+
5+
func Solution(target string) string {
6+
pos := [26][2]int{
7+
{0, 0}, {0, 1}, {0, 2}, {0, 3}, {0, 4},
8+
{1, 0}, {1, 1}, {1, 2}, {1, 3}, {1, 4},
9+
{2, 0}, {2, 1}, {2, 2}, {2, 3}, {2, 4},
10+
{3, 0}, {3, 1}, {3, 2}, {3, 3}, {3, 4},
11+
{4, 0}, {4, 1}, {4, 2}, {4, 3}, {4, 4},
12+
{5, 0},
13+
}
14+
buf := strings.Builder{}
15+
x, y := 0, 0
16+
for _, b := range target {
17+
bp := pos[b-'a']
18+
if bp[0] == x && bp[1] == y {
19+
buf.WriteByte('!')
20+
continue
21+
}
22+
dx, dy := bp[0]-x, bp[1]-y
23+
sx, sy := x, y
24+
x, y = bp[0], bp[1]
25+
if sx == 5 && sy == 0 {
26+
if dx < 0 {
27+
for ; dx < 0; dx++ {
28+
buf.WriteByte('U')
29+
}
30+
} else {
31+
for ; dx > 0; dx-- {
32+
buf.WriteByte('D')
33+
}
34+
}
35+
if dy < 0 {
36+
for ; dy < 0; dy++ {
37+
buf.WriteByte('L')
38+
}
39+
} else {
40+
for ; dy > 0; dy-- {
41+
buf.WriteByte('R')
42+
}
43+
}
44+
buf.WriteByte('!')
45+
continue
46+
}
47+
if dy < 0 {
48+
for ; dy < 0; dy++ {
49+
buf.WriteByte('L')
50+
}
51+
} else {
52+
for ; dy > 0; dy-- {
53+
buf.WriteByte('R')
54+
}
55+
}
56+
if dx < 0 {
57+
for ; dx < 0; dx++ {
58+
buf.WriteByte('U')
59+
}
60+
} else {
61+
for ; dx > 0; dx-- {
62+
buf.WriteByte('D')
63+
}
64+
}
65+
buf.WriteByte('!')
66+
}
67+
return buf.String()
568
}

leetcode/1101-1200/1138.Alphabet-Board-Path/Solution_test.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs string
14+
expect string
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", "leet", "RDD!RRRUU!!DDD!"},
17+
{"TestCase2", "code", "RR!RRDD!LUU!R!"},
1918
}
2019

2120
// 开始测试
@@ -30,10 +29,10 @@ func TestSolution(t *testing.T) {
3029
}
3130
}
3231

33-
// 压力测试
32+
// 压力测试
3433
func BenchmarkSolution(b *testing.B) {
3534
}
3635

37-
// 使用案列
36+
// 使用案列
3837
func ExampleSolution() {
3938
}

0 commit comments

Comments
 (0)