Skip to content

Commit 7a053c4

Browse files
author
robot
committed
feat: $1138
1 parent 8d01fb0 commit 7a053c4

File tree

3 files changed

+121
-0
lines changed

3 files changed

+121
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,7 @@ leetcode 题解,记录自己的 leetcode 解题之路。
386386
- [1104. 二叉树寻路](./problems/1104.path-in-zigzag-labelled-binary-tree.md)
387387
- [1129. 颜色交替的最短路径](./problems/1129.shortest-path-with-alternating-colors.md)
388388
- [1131.绝对值表达式的最大值](./problems/1131.maximum-of-absolute-value-expression.md)
389+
- [1138. 字母板上的路径](./problems/1138.alphabet-board-path.md)
389390
- [1186. 删除一次得到子数组最大和](./problems/1186.maximum-subarray-sum-with-one-deletion.md)
390391
- [1218. 最长定差子序列](./problems/1218.longest-arithmetic-subsequence-of-given-difference.md)
391392
- [1227. 飞机座位分配概率](./problems/1227.airplane-seat-assignment-probability.md) 👍

SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,7 @@
237237
- [1104. 二叉树寻路](./problems/1104.path-in-zigzag-labelled-binary-tree.md) 👍
238238
- [1129. 颜色交替的最短路径](./problems/1129.shortest-path-with-alternating-colors.md)
239239
- [1131.绝对值表达式的最大值](./problems/1131.maximum-of-absolute-value-expression.md) 👍
240+
- [1138. 字母板上的路径](./problems/1138.alphabet-board-path.md)
240241
- [1186. 删除一次得到子数组最大和](./problems/1186.maximum-subarray-sum-with-one-deletion.md) 👍
241242
- [1218. 最长定差子序列](./problems/1218.longest-arithmetic-subsequence-of-given-difference.md) 👍
242243
- [1227. 飞机座位分配概率](./problems/1227.airplane-seat-assignment-probability.md) 👍

problems/1138.alphabet-board-path.md

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
## 题目地址(1138. 字母板上的路径)
2+
3+
https://leetcode-cn.com/problems/alphabet-board-path/
4+
5+
## 题目描述
6+
7+
```
8+
我们从一块字母板上的位置 (0, 0) 出发,该坐标对应的字符为 board[0][0]。
9+
10+
在本题里,字母板为board = ["abcde", "fghij", "klmno", "pqrst", "uvwxy", "z"],如下所示。
11+
12+
我们可以按下面的指令规则行动:
13+
14+
如果方格存在,'U' 意味着将我们的位置上移一行;
15+
如果方格存在,'D' 意味着将我们的位置下移一行;
16+
如果方格存在,'L' 意味着将我们的位置左移一列;
17+
如果方格存在,'R' 意味着将我们的位置右移一列;
18+
'!' 会把在我们当前位置 (r, c) 的字符 board[r][c] 添加到答案中。
19+
20+
(注意,字母板上只存在有字母的位置。)
21+
22+
返回指令序列,用最小的行动次数让答案和目标 target 相同。你可以返回任何达成目标的路径。
23+
24+
 
25+
26+
示例 1:
27+
28+
输入:target = "leet"
29+
输出:"DDR!UURRR!!DDD!"
30+
31+
32+
示例 2:
33+
34+
输入:target = "code"
35+
输出:"RR!DDRR!UUL!R!"
36+
37+
38+
 
39+
40+
提示:
41+
42+
1 <= target.length <= 100
43+
target 仅含有小写英文字母。
44+
```
45+
46+
## 前置知识
47+
48+
- 矩阵
49+
50+
## 公司
51+
52+
- 暂无
53+
54+
## 思路
55+
56+
首先我们需要明确三点:
57+
58+
1. 题目中的字母板 board 是确认的,即题目给出的 board。如果题目的 board 是动态的,参数给出的,那么难度会加大。
59+
2. 对于 target。我们需要先录入 target 第一个字母,类似 xxxx! 的序列。然后是第二个字母 。。。而不能以其他顺序录入,否则难度也会加大。
60+
3. 如果当前的位置是 (x,y), 当前需要录入的字母位于 board 的 (tx, ty)。 那么有如下**最短**路径可能:
61+
- 先右再下或者先下后右,前提是 (tx, ty) 在 (x,y)右下。
62+
- 先左再下或者先下后左,前提是 (tx, ty) 在 (x,y)左下。
63+
- 先右再上或者先上后右,前提是 (tx, ty) 在 (x,y)右上。
64+
- 先左再上或者先上后左,前提是 (tx, ty) 在 (x,y)左上。
65+
66+
由于题目要求返回任意满足题意的路径,因此我们无论采用哪种都可以。
67+
68+
## 关键点
69+
70+
- 理解题意
71+
- 矩阵坐标映射
72+
73+
## 代码
74+
75+
- 语言支持:Python3
76+
77+
Python3 Code:
78+
79+
```python
80+
81+
class Solution:
82+
def alphabetBoardPath(self, target: str) -> str:
83+
board = []
84+
for i in range(5):
85+
for j in range(5):
86+
board.append((i,j))
87+
board.append((5,0))
88+
last_x = last_y = 0
89+
ans = ''
90+
for c in target:
91+
nxt_x, nxt_y = board[ord(c)-ord('a')]
92+
up = max(0, last_x - nxt_x)
93+
down = max(0, nxt_x - last_x)
94+
left = max(0, last_y - nxt_y)
95+
right = max(0, nxt_y - last_y)
96+
ans += 'U'*up + 'L'*left + 'D'*down + 'R'*right + '!'
97+
last_x, last_y = nxt_x, nxt_y
98+
return ans
99+
100+
101+
102+
```
103+
104+
**复杂度分析**
105+
106+
令 n 为 target 长度。
107+
108+
- 时间复杂度:构建 board 需要常数的时间,遍历 target 需要 n 的时间,因此时间复杂度为 $O(n)$
109+
- 空间复杂度:board 长度为固定的 26,因此空间复杂度为 $O(1)$
110+
111+
> 此题解由 [力扣刷题插件](https://leetcode-pp.github.io/leetcode-cheat/?tab=solution-template) 自动生成。
112+
113+
力扣的小伙伴可以[关注我](https://leetcode-cn.com/u/fe-lucifer/),这样就会第一时间收到我的动态啦~
114+
115+
以上就是本文的全部内容了。大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 40K star 啦。大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。
116+
117+
关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。
118+
119+
![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg)

0 commit comments

Comments
 (0)