-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1138.字母板上的路径.js
More file actions
84 lines (82 loc) · 1.5 KB
/
1138.字母板上的路径.js
File metadata and controls
84 lines (82 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/*
* @lc app=leetcode.cn id=1138 lang=javascript
*
* [1138] 字母板上的路径
*/
// @lc code=start
/**
* @param {string} target
* @return {string}
*/
var alphabetBoardPath = function (target) {
const map = new Map([
["a", [0, 0]],
["b", [0, 1]],
["c", [0, 2]],
["d", [0, 3]],
["e", [0, 4]],
["f", [1, 0]],
["g", [1, 1]],
["h", [1, 2]],
["i", [1, 3]],
["j", [1, 4]],
["k", [2, 0]],
["l", [2, 1]],
["m", [2, 2]],
["n", [2, 3]],
["o", [2, 4]],
["p", [3, 0]],
["q", [3, 1]],
["r", [3, 2]],
["s", [3, 3]],
["t", [3, 4]],
["u", [4, 0]],
["v", [4, 1]],
["w", [4, 2]],
["x", [4, 3]],
["y", [4, 4]],
["z", [5, 0]],
]);
let local = [0, 0];
let res = "";
for (let i = 0; i < target.length; i++) {
let current = map.get(target[i]);
let x = current[1] - local[1];
let y = current[0] - local[0];
if (local[0] === 5) {
res += writeY(y);
res += writeX(x);
} else {
res += writeX(x);
res += writeY(y);
}
res += "!";
local = current;
}
return res;
};
function writeX(x) {
let res = "";
for (let j = 0; j < Math.abs(x); j++) {
if (x > 0) {
res += "R";
} else {
res += "L";
}
}
return res;
}
function writeY(y) {
let res = "";
for (let j = 0; j < Math.abs(y); j++) {
if (y > 0) {
res += "D";
} else {
res += "U";
}
}
return res;
}
const target = "zb";
console.log(alphabetBoardPath(target));
// @lc code=end