Skip to content

Commit 64843c1

Browse files
committed
feat: add solutions to lc problem: No.0006
No.0006.Zigzag Conversion
1 parent e0b1472 commit 64843c1

File tree

2 files changed

+316
-64
lines changed

2 files changed

+316
-64
lines changed

solution/0000-0099/0006.Zigzag Conversion/README.md

Lines changed: 162 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,35 @@ P I
6464

6565
<!-- 这里可写通用的实现逻辑 -->
6666

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+
6775
<!-- tabs:start -->
6876

6977
### **Python3**
7078

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

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+
7396
```python
7497
class Solution:
7598
def convert(self, s: str, numRows: int) -> str:
@@ -93,6 +116,27 @@ class Solution:
93116

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

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+
96140
```java
97141
class Solution {
98142
public String convert(String s, int numRows) {
@@ -120,6 +164,31 @@ class Solution {
120164

121165
### **C++**
122166

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+
123192
```cpp
124193
class Solution {
125194
public:
@@ -142,42 +211,26 @@ public:
142211
};
143212
```
144213

145-
### **C#**
146-
147-
```cs
148-
using System.Collections.Generic;
149-
using System.Linq;
214+
### **Go**
150215

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))
176231
}
177232
```
178233

179-
### **Go**
180-
181234
```go
182235
func convert(s string, numRows int) string {
183236
if numRows == 1 {
@@ -203,6 +256,30 @@ func convert(s string, numRows int) string {
203256

204257
### **JavaScript**
205258

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+
206283
```js
207284
/**
208285
* @param {string} s
@@ -234,6 +311,25 @@ var convert = function (s, numRows) {
234311

235312
### **TypeScript**
236313

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+
237333
```ts
238334
function convert(s: string, numRows: number): string {
239335
if (numRows === 1) {
@@ -257,6 +353,40 @@ function convert(s: string, numRows: number): string {
257353
}
258354
```
259355

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+
260390
### **Rust**
261391

262392
```rust

0 commit comments

Comments
 (0)