-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAlphabetBoardPath.java
More file actions
78 lines (58 loc) · 1.72 KB
/
AlphabetBoardPath.java
File metadata and controls
78 lines (58 loc) · 1.72 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
package com.leetcode.map;
public class AlphabetBoardPath {
public String alphabetBoardPath(String target){
StringBuilder sb = new StringBuilder();
int curRow=0;
int curCol=0;
for(int i=0;i<target.length();i++){
char tc = target.charAt(i);
int row=(tc-'a')/5;
int col=(tc-'a')%5;
if(row==5&&col<curCol){
for(int j=curRow;j<4;j++){
sb.append('D');
}
for(int j=curCol;j>col;j--){
sb.append('L');
}
sb.append('D');
}else{
if(curRow>row){
for(int j=curRow;j>row;j--){
sb.append('U');
}
}else if(curRow<row){
for(int j=curRow;j<row;j++){
sb.append('D');
}
}
if(curCol>col){
for(int j=curCol;j>col;j--){
sb.append('L');
}
}else if(curCol<col){
for(int j=curCol;j<col;j++){
sb.append('R');
}
}
}
curRow=row;
curCol=col;
sb.append('!');
}
return sb.toString();
}
public static void main(String[] args) {
AlphabetBoardPath alphabet = new AlphabetBoardPath();
//"DDDDD!UUUUURRR!DDDDLLLD!"
System.out.println(alphabet.alphabetBoardPath("zdz"));
/***
* "abcde",
* "fghij",
* "klmno",
* "pqrst",
* "uvwxy",
* "z"
*/
}
}