File tree Expand file tree Collapse file tree 6 files changed +328
-32
lines changed
solution/1100-1199/1138.Alphabet Board Path Expand file tree Collapse file tree 6 files changed +328
-32
lines changed Original file line number Diff line number Diff line change 55
55
56
56
<!-- 这里可写通用的实现逻辑 -->
57
57
58
+ ** 方法一:模拟**
59
+
60
+ 从起点 $(0, 0)$ 出发,模拟每一步的移动,将每一步的移动结果拼接到答案中。注意移动的方向遵循“左、上、右、下”的顺序。
61
+
62
+ 时间复杂度 $O(n)$,其中 $n$ 是字符串 $target$ 的长度,需要遍历字符串 $target$ 中的每一个字符。忽略答案的空间消耗,空间复杂度 $O(1)$。
63
+
58
64
<!-- tabs:start -->
59
65
60
66
### ** Python3**
61
67
62
68
<!-- 这里可写当前语言的特殊实现逻辑 -->
63
69
64
70
``` 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)
66
92
```
67
93
68
94
### ** Java**
69
95
70
96
<!-- 这里可写当前语言的特殊实现逻辑 -->
71
97
72
98
``` 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
+ ```
73
162
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
+ }
74
192
```
75
193
76
194
### ** ...**
Original file line number Diff line number Diff line change 56
56
### ** Python3**
57
57
58
58
``` 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)
60
80
```
61
81
62
82
### ** Java**
63
83
64
84
``` 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
+ ```
65
148
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
+ }
66
178
```
67
179
68
180
### ** ...**
Original file line number Diff line number Diff line change
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
+ };
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change 1
1
class Solution {
2
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
- }
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' );
13
11
}
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' );
19
15
}
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' );
25
19
}
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' );
31
23
}
32
- sb .append ('!' );
33
- x = dx ;
34
- y = dy ;
24
+ ans .append ("!" );
35
25
}
36
- return sb .toString ();
26
+ return ans .toString ();
37
27
}
38
- }
28
+ }
You can’t perform that action at this time.
0 commit comments