Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 4941ec4

Browse files
authored
Create Solution.java
1 parent decbbee commit 4941ec4

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
class Solution {
2+
public String alphabetBoardPath(String target) {
3+
StringBuilder sb = new StringBuilder();
4+
int x = 0, y = 0;
5+
for (char c : target.toCharArray()) {
6+
int dx = (c - 'a') / 5;
7+
int dy = (c - 'a') % 5;
8+
if (dy < y) {
9+
int n = y - dy;
10+
while (n-- > 0) {
11+
sb.append('L');
12+
}
13+
}
14+
if (dx > x) {
15+
int n = dx - x;
16+
while (n-- > 0) {
17+
sb.append('D');
18+
}
19+
}
20+
if (dx < x) {
21+
int n = x - dx;
22+
while (n-- > 0) {
23+
sb.append('U');
24+
}
25+
}
26+
if (dy > y) {
27+
int n = dy - y;
28+
while (n-- > 0) {
29+
sb.append('R');
30+
}
31+
}
32+
sb.append('!');
33+
x = dx;
34+
y = dy;
35+
}
36+
return sb.toString();
37+
}
38+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /