64
64
65
65
<!-- 这里可写通用的实现逻辑 -->
66
66
67
+ ** 方法一:模拟**
68
+
69
+ 我们用一个二维数组 $g$ 来模拟 $Z$ 字形排列的过程,其中 $g[ i] [ j ] $ 表示第 $i$ 行第 $j$ 列的字符。初始时 $i=0$,另外我们定义一个方向变量 $k$,初始时 $k=-1$,表示向上走。
70
+
71
+ 我们从左到右遍历字符串 $s$,每次遍历到一个字符 $c$,将其追加到 $g[ i] $ 中,如果此时 $i=0$ 或者 $i=numRows-1$,说明当前字符位于 $Z$ 字形排列的拐点,我们将 $k$ 的值反转,即 $k=-k$。接下来,我们将 $i$ 的值更新为 $i+k$,即向上或向下移动一行。继续遍历下一个字符,直到遍历完字符串 $s$,我们返回 $g$ 中所有行拼接后的字符串即可。
72
+
73
+ 时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为字符串 $s$ 的长度。
74
+
67
75
<!-- tabs:start -->
68
76
69
77
### ** Python3**
70
78
71
79
<!-- 这里可写当前语言的特殊实现逻辑 -->
72
80
81
+ ``` python
82
+ class Solution :
83
+ def convert (self , s : str , numRows : int ) -> str :
84
+ if numRows == 1 :
85
+ return s
86
+ g = [[] for _ in range (numRows)]
87
+ i, k = 0 , - 1
88
+ for c in s:
89
+ g[i].append(c)
90
+ if i == 0 or i == numRows - 1 :
91
+ k = - k
92
+ i += k
93
+ return ' ' .join(chain(* g))
94
+ ```
95
+
73
96
``` python
74
97
class Solution :
75
98
def convert (self , s : str , numRows : int ) -> str :
@@ -93,6 +116,27 @@ class Solution:
93
116
94
117
<!-- 这里可写当前语言的特殊实现逻辑 -->
95
118
119
+ ``` java
120
+ class Solution {
121
+ public String convert (String s , int numRows ) {
122
+ if (numRows == 1 ) {
123
+ return s;
124
+ }
125
+ StringBuilder [] g = new StringBuilder [numRows];
126
+ Arrays . setAll(g, k - > new StringBuilder ());
127
+ int i = 0 , k = - 1 ;
128
+ for (char c : s. toCharArray()) {
129
+ g[i]. append(c);
130
+ if (i == 0 || i == numRows - 1 ) {
131
+ k = - k;
132
+ }
133
+ i += k;
134
+ }
135
+ return String . join(" " , g);
136
+ }
137
+ }
138
+ ```
139
+
96
140
``` java
97
141
class Solution {
98
142
public String convert (String s , int numRows ) {
@@ -120,6 +164,31 @@ class Solution {
120
164
121
165
### ** C++**
122
166
167
+ ``` cpp
168
+ class Solution {
169
+ public:
170
+ string convert(string s, int numRows) {
171
+ if (numRows == 1) {
172
+ return s;
173
+ }
174
+ vector<string > g(numRows);
175
+ int i = 0, k = -1;
176
+ for (char c : s) {
177
+ g[ i] += c;
178
+ if (i == 0 || i == numRows - 1) {
179
+ k = -k;
180
+ }
181
+ i += k;
182
+ }
183
+ string ans;
184
+ for (auto& t : g) {
185
+ ans += t;
186
+ }
187
+ return ans;
188
+ }
189
+ };
190
+ ```
191
+
123
192
```cpp
124
193
class Solution {
125
194
public:
@@ -142,42 +211,26 @@ public:
142
211
};
143
212
```
144
213
145
- ### **C#**
146
-
147
- ```cs
148
- using System.Collections.Generic;
149
- using System.Linq;
214
+ ### ** Go**
150
215
151
- public class Solution {
152
- public string Convert(string s, int numRows) {
153
- if (numRows == 1) return s;
154
- if (numRows > s.Length) numRows = s.Length;
155
- var rows = new List<char>[numRows];
156
- var i = 0;
157
- var j = 0;
158
- var down = true;
159
- while (i < s.Length)
160
- {
161
- if (rows[j] == null)
162
- {
163
- rows[j] = new List<char>();
164
- }
165
- rows[j].Add(s[i]);
166
- j = j + (down ? 1 : -1);
167
- if (j == numRows || j < 0)
168
- {
169
- down = !down;
170
- j = j + (down ? 2 : -2);
171
- }
172
- ++i;
173
- }
174
- return new string(rows.SelectMany(row => row).ToArray());
175
- }
216
+ ``` go
217
+ func convert (s string , numRows int ) string {
218
+ if numRows == 1 {
219
+ return s
220
+ }
221
+ g := make ([][]byte , numRows)
222
+ i , k := 0 , -1
223
+ for _ , c := range s {
224
+ g[i] = append (g[i], byte (c))
225
+ if i == 0 || i == numRows-1 {
226
+ k = -k
227
+ }
228
+ i += k
229
+ }
230
+ return string (bytes.Join (g, nil ))
176
231
}
177
232
```
178
233
179
- ### ** Go**
180
-
181
234
``` go
182
235
func convert (s string , numRows int ) string {
183
236
if numRows == 1 {
@@ -203,6 +256,30 @@ func convert(s string, numRows int) string {
203
256
204
257
### ** JavaScript**
205
258
259
+ ``` js
260
+ /**
261
+ * @param {string} s
262
+ * @param {number} numRows
263
+ * @return {string}
264
+ */
265
+ var convert = function (s , numRows ) {
266
+ if (numRows == 1 ) {
267
+ return s;
268
+ }
269
+ const g = new Array (numRows).fill (_).map (() => []);
270
+ let i = 0 ;
271
+ let k = - 1 ;
272
+ for (const c of s) {
273
+ g[i].push (c);
274
+ if (i == 0 || i == numRows - 1 ) {
275
+ k = - k;
276
+ }
277
+ i += k;
278
+ }
279
+ return g .flat ().join (' ' );
280
+ };
281
+ ```
282
+
206
283
``` js
207
284
/**
208
285
* @param {string} s
@@ -234,6 +311,25 @@ var convert = function (s, numRows) {
234
311
235
312
### ** TypeScript**
236
313
314
+ ``` ts
315
+ function convert(s : string , numRows : number ): string {
316
+ if (numRows === 1 ) {
317
+ return s ;
318
+ }
319
+ const g: string [][] = new Array (numRows ).fill (0 ).map (() => []);
320
+ let i = 0 ;
321
+ let k = - 1 ;
322
+ for (const c of s ) {
323
+ g [i ].push (c );
324
+ if (i === numRows - 1 || i === 0 ) {
325
+ k = - k ;
326
+ }
327
+ i += k ;
328
+ }
329
+ return g .flat ().join (' ' );
330
+ }
331
+ ```
332
+
237
333
``` ts
238
334
function convert(s : string , numRows : number ): string {
239
335
if (numRows === 1 ) {
@@ -257,6 +353,40 @@ function convert(s: string, numRows: number): string {
257
353
}
258
354
```
259
355
356
+ ### ** C#**
357
+
358
+ ``` cs
359
+ using System .Collections .Generic ;
360
+ using System .Linq ;
361
+
362
+ public class Solution {
363
+ public string Convert (string s , int numRows ) {
364
+ if (numRows == 1 ) return s ;
365
+ if (numRows > s .Length ) numRows = s .Length ;
366
+ var rows = new List <char >[numRows ];
367
+ var i = 0 ;
368
+ var j = 0 ;
369
+ var down = true ;
370
+ while (i < s .Length )
371
+ {
372
+ if (rows [j ] == null )
373
+ {
374
+ rows [j ] = new List <char >();
375
+ }
376
+ rows [j ].Add (s [i ]);
377
+ j = j + (down ? 1 : - 1 );
378
+ if (j == numRows || j < 0 )
379
+ {
380
+ down = ! down ;
381
+ j = j + (down ? 2 : - 2 );
382
+ }
383
+ ++ i ;
384
+ }
385
+ return new string (rows .SelectMany (row => row ).ToArray ());
386
+ }
387
+ }
388
+ ```
389
+
260
390
### ** Rust**
261
391
262
392
``` rust
0 commit comments