Skip to content

Commit 3f25c35

Browse files
committed
2019-07-28
1 parent 2513ec7 commit 3f25c35

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
class Solution(object):
2+
def alphabetBoardPath(self, target):
3+
"""
4+
:type target: str
5+
:rtype: str
6+
"""
7+
board = ["abcde",
8+
"fghij",
9+
"klmno",
10+
"pqrst",
11+
"uvwxy",
12+
"z"]
13+
14+
dic = dict()
15+
for i in range(len(board)):
16+
for j in range(len(board[0])):
17+
18+
dic[board[i][j]] = [i, j]
19+
if i == 5:
20+
break
21+
22+
x0, y0 = 0,0
23+
res = ""
24+
for char in target:
25+
tmp = ""
26+
des = dic[char]
27+
x1, y1 = des[0], des[1]
28+
if x0 == x1 and y0 == y1:
29+
res += "!"
30+
continue
31+
if x0 < x1:
32+
tmp += "D" * (x1 - x0)
33+
elif x0 > x1:
34+
tmp += "U" * (x0 - x1)
35+
if x1 == 5 and tmp and tmp[-1] == "D":
36+
tmp = tmp[:-1]
37+
if y0 < y1:
38+
tmp += "R" * (y1 - y0)
39+
elif y1 < y0:
40+
tmp += "L" * (y0 - y1)
41+
x0 = x1
42+
y0 = y1
43+
if x1 == 5:
44+
tmp += "D"
45+
tmp += "!"
46+
res += tmp
47+
return res
48+
49+
50+

0 commit comments

Comments
 (0)