forked from luliyucoordinate/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1138.js
31 lines (31 loc) · 766 Bytes
/
1138.js
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
var alphabetBoardPath = function(target) {
var res = "";
var x = 0, y = 0;
for (let ch of target) {
var x1 = Math.floor((ch.charCodeAt(0) - 97)/5), y1 = (ch.charCodeAt(0) - 97)%5;
var l = x1 - x, c = y1 - y;
if (l < 0) {
for (var i = 0; i < -l; ++i) {
res += "U";
}
}
if (c < 0) {
for (var i = 0; i < -c; ++i) {
res += "L";
}
}
if (l > 0) {
for (var i = 0; i < l; ++i) {
res += "D";
}
}
if (c > 0) {
for (var i = 0; i < c; ++i) {
res += "R";
}
}
res += "!";
x = x1, y = y1;
}
return res;
};