Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added leetcode/1101-1200/1138.Alphabet-Board-Path/1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
37 changes: 23 additions & 14 deletions leetcode/1101-1200/1138.Alphabet-Board-Path/README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,37 @@
# [1138.Alphabet Board Path][title]

> [!WARNING|style:flat]
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)

## Description
On an alphabet board, we start at position `(0, 0)`, corresponding to character `board[0][0]`.

Here, `board = ["abcde", "fghij", "klmno", "pqrst", "uvwxy", "z"]`, as shown in the diagram below.

![1](./1.png)

We may make the following moves:

- `'U'` moves our position up one row, if the position exists on the board;
- `'D'` moves our position down one row, if the position exists on the board;
- `'L'` moves our position left one column, if the position exists on the board;
- `'R'` moves our position right one column, if the position exists on the board;
- `'!'` adds the character `board[r][c]` at our current position `(r, c)` to the answer.

(Here, the only positions that exist on the board are positions with letters on them.)

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.

**Example 1:**

```
Input: a = "11", b = "1"
Output: "100"
Input: target = "leet"
Output: "DDR!UURRR!!DDD!"
```

## 题意
> ...

## 题解
**Example 2:**

### 思路1
> ...
Alphabet Board Path
```go
```

Input: target = "code"
Output: "RR!DDRR!UUL!R!"
```

## 结语

Expand Down
67 changes: 65 additions & 2 deletions leetcode/1101-1200/1138.Alphabet-Board-Path/Solution.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,68 @@
package Solution

func Solution(x bool) bool {
return x
import "strings"

func Solution(target string) string {
pos := [26][2]int{
{0, 0}, {0, 1}, {0, 2}, {0, 3}, {0, 4},
{1, 0}, {1, 1}, {1, 2}, {1, 3}, {1, 4},
{2, 0}, {2, 1}, {2, 2}, {2, 3}, {2, 4},
{3, 0}, {3, 1}, {3, 2}, {3, 3}, {3, 4},
{4, 0}, {4, 1}, {4, 2}, {4, 3}, {4, 4},
{5, 0},
}
buf := strings.Builder{}
x, y := 0, 0
for _, b := range target {
bp := pos[b-'a']
if bp[0] == x && bp[1] == y {
buf.WriteByte('!')
continue
}
dx, dy := bp[0]-x, bp[1]-y
sx, sy := x, y
x, y = bp[0], bp[1]
if sx == 5 && sy == 0 {
if dx < 0 {
for ; dx < 0; dx++ {
buf.WriteByte('U')
}
} else {
for ; dx > 0; dx-- {
buf.WriteByte('D')
}
}
if dy < 0 {
for ; dy < 0; dy++ {
buf.WriteByte('L')
}
} else {
for ; dy > 0; dy-- {
buf.WriteByte('R')
}
}
buf.WriteByte('!')
continue
}
if dy < 0 {
for ; dy < 0; dy++ {
buf.WriteByte('L')
}
} else {
for ; dy > 0; dy-- {
buf.WriteByte('R')
}
}
if dx < 0 {
for ; dx < 0; dx++ {
buf.WriteByte('U')
}
} else {
for ; dx > 0; dx-- {
buf.WriteByte('D')
}
}
buf.WriteByte('!')
}
return buf.String()
}
13 changes: 6 additions & 7 deletions leetcode/1101-1200/1138.Alphabet-Board-Path/Solution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@ func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
inputs string
expect string
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", "leet", "RDD!RRRUU!!DDD!"},
{"TestCase2", "code", "RR!RRDD!LUU!R!"},
}

// 开始测试
Expand All @@ -30,10 +29,10 @@ func TestSolution(t *testing.T) {
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}
Loading