diff --git a/leetcode/1101-1200/1138.Alphabet-Board-Path/1.png b/leetcode/1101-1200/1138.Alphabet-Board-Path/1.png new file mode 100644 index 000000000..db370622e Binary files /dev/null and b/leetcode/1101-1200/1138.Alphabet-Board-Path/1.png differ diff --git a/leetcode/1101-1200/1138.Alphabet-Board-Path/README.md b/leetcode/1101-1200/1138.Alphabet-Board-Path/README.md index 9573053cc..dde2e8a94 100644 --- a/leetcode/1101-1200/1138.Alphabet-Board-Path/README.md +++ b/leetcode/1101-1200/1138.Alphabet-Board-Path/README.md @@ -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!" +``` ## 结语 diff --git a/leetcode/1101-1200/1138.Alphabet-Board-Path/Solution.go b/leetcode/1101-1200/1138.Alphabet-Board-Path/Solution.go index d115ccf5e..f63db5a9e 100644 --- a/leetcode/1101-1200/1138.Alphabet-Board-Path/Solution.go +++ b/leetcode/1101-1200/1138.Alphabet-Board-Path/Solution.go @@ -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() } diff --git a/leetcode/1101-1200/1138.Alphabet-Board-Path/Solution_test.go b/leetcode/1101-1200/1138.Alphabet-Board-Path/Solution_test.go index 14ff50eb4..3027be314 100644 --- a/leetcode/1101-1200/1138.Alphabet-Board-Path/Solution_test.go +++ b/leetcode/1101-1200/1138.Alphabet-Board-Path/Solution_test.go @@ -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!"}, } // 开始测试 @@ -30,10 +29,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }