forked from luliyucoordinate/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
1138.go
34 lines (31 loc) · 721 Bytes
/
1138.go
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
func alphabetBoardPath(target string) string {
res := ""
x, y := 0, 0
for _, v := range []byte(target) {
x1, y1 := int(v - 'a') / 5, int(v - 'a') % 5
l, c := x1 - x, y1 - y
if c < 0 {
for i := 0; i < -c; i++ {
res += "L"
}
}
if l < 0 {
for i := 0; i < -l; i++ {
res += "U"
}
}
if c > 0 {
for i := 0; i < c; i++ {
res += "R"
}
}
if l > 0 {
for i := 0; i < l; i++ {
res += "D"
}
}
res += "!"
x, y = x1, y1
}
return res
}