File tree Expand file tree Collapse file tree 2 files changed +38
-0
lines changed Expand file tree Collapse file tree 2 files changed +38
-0
lines changed Original file line number Diff line number Diff line change 196
196
| 988| [ 从叶结点开始的最小字符串] ( https://leetcode.cn/problems/smallest-string-starting-from-leaf/ ) | [ JavaScript] ( ./algorithms/smallest-string-starting-from-leaf.js ) | Medium|
197
197
| 1002| [ 查找共用字符] ( https://leetcode.cn/problems/find-common-characters/ ) | [ JavaScript] ( ./algorithms/find-common-characters.js ) | Easy|
198
198
| 1005| [ K 次取反后最大化的数组和] ( https://leetcode.cn/problems/maximize-sum-of-array-after-k-negations/ ) | [ JavaScript] ( ./algorithms/maximize-sum-of-array-after-k-negations.js ) | Easy|
199
+ | 1138| [ 字母板上的路径] ( https://leetcode.cn/problems/alphabet-board-path/ ) | [ JavaScript] ( ./algorithms/alphabet-board-path.js ) | Medium|
199
200
| 1038| [ 从二叉搜索树到更大和树] ( https://leetcode.cn/problems/binary-search-tree-to-greater-sum-tree/ ) | [ JavaScript] ( ./algorithms/binary-search-tree-to-greater-sum-tree.js ) | Medium|
200
201
| 1047| [ 删除字符串中的所有相邻重复项] ( https://leetcode.cn/problems/remove-all-adjacent-duplicates-in-string/ ) | [ JavaScript] ( ./algorithms/remove-all-adjacent-duplicates-in-string.js ) | Easy|
201
202
| 1233| [ 删除子文件夹] ( https://leetcode.cn/problems/remove-sub-folders-from-the-filesystem/ ) | [ JavaScript] ( ./algorithms/remove-sub-folders-from-the-filesystem.js ) | Medium|
Original file line number Diff line number Diff line change
1
+ /**
2
+ * 1138. 字母板上的路径
3
+ * @param {string } target
4
+ * @return {string }
5
+ */
6
+ var alphabetBoardPath = function ( target ) {
7
+ let result = "" ;
8
+ let prevRow = 0 ;
9
+ let prevCol = 0 ;
10
+
11
+ for ( let i = 0 ; i < target . length ; i ++ ) {
12
+ const index = target [ i ] . charCodeAt ( ) - 97 ;
13
+ const currRow = Math . floor ( index / 5 ) ;
14
+ const currCol = index % 5 ;
15
+
16
+ const rowDiff = Math . abs ( currRow - prevRow ) ;
17
+ const colDiff = Math . abs ( currCol - prevCol ) ;
18
+
19
+ // 优先 `上移` 和 `左移` 避免 'z' 字符的问题
20
+ if ( currRow < prevRow ) {
21
+ result += "U" . repeat ( rowDiff ) ;
22
+ }
23
+ if ( currCol < prevCol ) {
24
+ result += "L" . repeat ( colDiff ) ;
25
+ }
26
+ if ( currRow > prevRow ) {
27
+ result += "D" . repeat ( rowDiff ) ;
28
+ }
29
+ if ( currCol > prevCol ) {
30
+ result += "R" . repeat ( colDiff ) ;
31
+ }
32
+ result += "!" ;
33
+ prevRow = currRow ;
34
+ prevCol = currCol ;
35
+ }
36
+ return result ;
37
+ } ;
You can’t perform that action at this time.
0 commit comments