Skip to content

Commit 8bd8299

Browse files
committed
feat: add solutions to lc problem: No.1138
No.1138.Alphabet Board Path
1 parent e44b6d3 commit 8bd8299

File tree

6 files changed

+328
-32
lines changed

6 files changed

+328
-32
lines changed

solution/1100-1199/1138.Alphabet Board Path/README.md

Lines changed: 119 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,22 +55,140 @@
5555

5656
<!-- 这里可写通用的实现逻辑 -->
5757

58+
**方法一:模拟**
59+
60+
从起点 $(0, 0)$ 出发,模拟每一步的移动,将每一步的移动结果拼接到答案中。注意移动的方向遵循“左、上、右、下”的顺序。
61+
62+
时间复杂度 $O(n)$,其中 $n$ 是字符串 $target$ 的长度,需要遍历字符串 $target$ 中的每一个字符。忽略答案的空间消耗,空间复杂度 $O(1)$。
63+
5864
<!-- tabs:start -->
5965

6066
### **Python3**
6167

6268
<!-- 这里可写当前语言的特殊实现逻辑 -->
6369

6470
```python
65-
71+
class Solution:
72+
def alphabetBoardPath(self, target: str) -> str:
73+
i = j = 0
74+
ans = []
75+
for c in target:
76+
v = ord(c) - ord("a")
77+
x, y = v // 5, v % 5
78+
while j > y:
79+
j -= 1
80+
ans.append("L")
81+
while i > x:
82+
i -= 1
83+
ans.append("U")
84+
while j < y:
85+
j += 1
86+
ans.append("R")
87+
while i < x:
88+
i += 1
89+
ans.append("D")
90+
ans.append("!")
91+
return "".join(ans)
6692
```
6793

6894
### **Java**
6995

7096
<!-- 这里可写当前语言的特殊实现逻辑 -->
7197

7298
```java
99+
class Solution {
100+
public String alphabetBoardPath(String target) {
101+
StringBuilder ans = new StringBuilder();
102+
int i = 0, j = 0;
103+
for (int k = 0; k < target.length(); ++k) {
104+
int v = target.charAt(k) - 'a';
105+
int x = v / 5, y = v % 5;
106+
while (j > y) {
107+
--j;
108+
ans.append('L');
109+
}
110+
while (i > x) {
111+
--i;
112+
ans.append('U');
113+
}
114+
while (j < y) {
115+
++j;
116+
ans.append('R');
117+
}
118+
while (i < x) {
119+
++i;
120+
ans.append('D');
121+
}
122+
ans.append("!");
123+
}
124+
return ans.toString();
125+
}
126+
}
127+
```
128+
129+
### **C++**
130+
131+
```cpp
132+
class Solution {
133+
public:
134+
string alphabetBoardPath(string target) {
135+
string ans;
136+
int i = 0, j = 0;
137+
for (char& c : target) {
138+
int v = c - 'a';
139+
int x = v / 5, y = v % 5;
140+
while (j > y) {
141+
--j;
142+
ans += 'L';
143+
}
144+
while (i > x) {
145+
--i;
146+
ans += 'U';
147+
}
148+
while (j < y) {
149+
++j;
150+
ans += 'R';
151+
}
152+
while (i < x) {
153+
++i;
154+
ans += 'D';
155+
}
156+
ans += '!';
157+
}
158+
return ans;
159+
}
160+
};
161+
```
73162
163+
### **Go**
164+
165+
```go
166+
func alphabetBoardPath(target string) string {
167+
ans := []byte{}
168+
var i, j int
169+
for _, c := range target {
170+
v := int(c - 'a')
171+
x, y := v/5, v%5
172+
for j > y {
173+
j--
174+
ans = append(ans, 'L')
175+
}
176+
for i > x {
177+
i--
178+
ans = append(ans, 'U')
179+
}
180+
for j < y {
181+
j++
182+
ans = append(ans, 'R')
183+
}
184+
for i < x {
185+
i++
186+
ans = append(ans, 'D')
187+
}
188+
ans = append(ans, '!')
189+
}
190+
return string(ans)
191+
}
74192
```
75193

76194
### **...**

solution/1100-1199/1138.Alphabet Board Path/README_EN.md

Lines changed: 113 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,125 @@
5656
### **Python3**
5757

5858
```python
59-
59+
class Solution:
60+
def alphabetBoardPath(self, target: str) -> str:
61+
i = j = 0
62+
ans = []
63+
for c in target:
64+
v = ord(c) - ord("a")
65+
x, y = v // 5, v % 5
66+
while j > y:
67+
j -= 1
68+
ans.append("L")
69+
while i > x:
70+
i -= 1
71+
ans.append("U")
72+
while j < y:
73+
j += 1
74+
ans.append("R")
75+
while i < x:
76+
i += 1
77+
ans.append("D")
78+
ans.append("!")
79+
return "".join(ans)
6080
```
6181

6282
### **Java**
6383

6484
```java
85+
class Solution {
86+
public String alphabetBoardPath(String target) {
87+
StringBuilder ans = new StringBuilder();
88+
int i = 0, j = 0;
89+
for (int k = 0; k < target.length(); ++k) {
90+
int v = target.charAt(k) - 'a';
91+
int x = v / 5, y = v % 5;
92+
while (j > y) {
93+
--j;
94+
ans.append('L');
95+
}
96+
while (i > x) {
97+
--i;
98+
ans.append('U');
99+
}
100+
while (j < y) {
101+
++j;
102+
ans.append('R');
103+
}
104+
while (i < x) {
105+
++i;
106+
ans.append('D');
107+
}
108+
ans.append("!");
109+
}
110+
return ans.toString();
111+
}
112+
}
113+
```
114+
115+
### **C++**
116+
117+
```cpp
118+
class Solution {
119+
public:
120+
string alphabetBoardPath(string target) {
121+
string ans;
122+
int i = 0, j = 0;
123+
for (char& c : target) {
124+
int v = c - 'a';
125+
int x = v / 5, y = v % 5;
126+
while (j > y) {
127+
--j;
128+
ans += 'L';
129+
}
130+
while (i > x) {
131+
--i;
132+
ans += 'U';
133+
}
134+
while (j < y) {
135+
++j;
136+
ans += 'R';
137+
}
138+
while (i < x) {
139+
++i;
140+
ans += 'D';
141+
}
142+
ans += '!';
143+
}
144+
return ans;
145+
}
146+
};
147+
```
65148
149+
### **Go**
150+
151+
```go
152+
func alphabetBoardPath(target string) string {
153+
ans := []byte{}
154+
var i, j int
155+
for _, c := range target {
156+
v := int(c - 'a')
157+
x, y := v/5, v%5
158+
for j > y {
159+
j--
160+
ans = append(ans, 'L')
161+
}
162+
for i > x {
163+
i--
164+
ans = append(ans, 'U')
165+
}
166+
for j < y {
167+
j++
168+
ans = append(ans, 'R')
169+
}
170+
for i < x {
171+
i++
172+
ans = append(ans, 'D')
173+
}
174+
ans = append(ans, '!')
175+
}
176+
return string(ans)
177+
}
66178
```
67179

68180
### **...**
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution {
2+
public:
3+
string alphabetBoardPath(string target) {
4+
string ans;
5+
int i = 0, j = 0;
6+
for (char& c : target) {
7+
int v = c - 'a';
8+
int x = v / 5, y = v % 5;
9+
while (j > y) {
10+
--j;
11+
ans += 'L';
12+
}
13+
while (i > x) {
14+
--i;
15+
ans += 'U';
16+
}
17+
while (j < y) {
18+
++j;
19+
ans += 'R';
20+
}
21+
while (i < x) {
22+
++i;
23+
ans += 'D';
24+
}
25+
ans += '!';
26+
}
27+
return ans;
28+
}
29+
};
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
func alphabetBoardPath(target string) string {
2+
ans := []byte{}
3+
var i, j int
4+
for _, c := range target {
5+
v := int(c - 'a')
6+
x, y := v/5, v%5
7+
for j > y {
8+
j--
9+
ans = append(ans, 'L')
10+
}
11+
for i > x {
12+
i--
13+
ans = append(ans, 'U')
14+
}
15+
for j < y {
16+
j++
17+
ans = append(ans, 'R')
18+
}
19+
for i < x {
20+
i++
21+
ans = append(ans, 'D')
22+
}
23+
ans = append(ans, '!')
24+
}
25+
return string(ans)
26+
}
Lines changed: 20 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,28 @@
11
class Solution {
22
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-
}
3+
StringBuilder ans = new StringBuilder();
4+
int i = 0, j = 0;
5+
for (int k = 0; k < target.length(); ++k) {
6+
int v = target.charAt(k) - 'a';
7+
int x = v / 5, y = v % 5;
8+
while (j > y) {
9+
--j;
10+
ans.append('L');
1311
}
14-
if (dx > x) {
15-
int n = dx - x;
16-
while (n-- > 0) {
17-
sb.append('D');
18-
}
12+
while (i > x) {
13+
--i;
14+
ans.append('U');
1915
}
20-
if (dx < x) {
21-
int n = x - dx;
22-
while (n-- > 0) {
23-
sb.append('U');
24-
}
16+
while (j < y) {
17+
++j;
18+
ans.append('R');
2519
}
26-
if (dy > y) {
27-
int n = dy - y;
28-
while (n-- > 0) {
29-
sb.append('R');
30-
}
20+
while (i < x) {
21+
++i;
22+
ans.append('D');
3123
}
32-
sb.append('!');
33-
x = dx;
34-
y = dy;
24+
ans.append("!");
3525
}
36-
return sb.toString();
26+
return ans.toString();
3727
}
38-
}
28+
}

0 commit comments

Comments
 (0)