Skip to content

Commit 661b494

Browse files
committed
fix: show solutions
No.0006.ZigZag Conversion
1 parent d31d918 commit 661b494

File tree

2 files changed

+373
-1
lines changed

2 files changed

+373
-1
lines changed

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

Lines changed: 185 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,15 +71,199 @@ P I
7171
<!-- 这里可写当前语言的特殊实现逻辑 -->
7272

7373
```python
74-
74+
class Solution:
75+
def convert(self, s, numRows):
76+
"""
77+
:type s: str
78+
:type numRows: int
79+
:rtype: str
80+
"""
81+
82+
if numRows == 0:
83+
return ""
84+
elif numRows == 1:
85+
return s
86+
87+
Ret = [[] for i in range(numRows)]
88+
i = 0
89+
while i < len(s):
90+
j = 0
91+
while i < len(s) and j < numRows: # Vertical lines
92+
Ret[j].append(s[i])
93+
i += 1
94+
j += 1
95+
j -= 2
96+
while i < len(s) and j > 0: # Diagonal lines
97+
Ret[j].append(s[i])
98+
j -= 1
99+
i += 1
100+
101+
return "".join(["".join(row) for row in Ret])
75102
```
76103

77104
### **Java**
78105

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

81108
```java
109+
class Solution {
110+
public String convert(String s, int numRows) {
111+
if (numRows == 1) return s;
112+
StringBuilder result = new StringBuilder();
113+
int group = 2 * numRows - 2;
114+
for (int i = 1; i <= numRows; i++) {
115+
int interval = 2 * numRows - 2 * i;
116+
if (i == numRows) interval = 2 * numRows - 2;
117+
int index = i;
118+
while (index <= s.length()) {
119+
result.append(s.charAt(index - 1));
120+
index += interval;
121+
interval = group - interval;
122+
if (interval == 0) interval = group;
123+
}
124+
}
125+
return result.toString();
126+
}
127+
}
128+
```
129+
130+
### **C++**
131+
132+
```cpp
133+
// @ID:6. ZigZag Conversion
134+
// @author:jxdeng3989
135+
136+
class Solution {
137+
public:
138+
string convert(string s, int numRows) {
139+
string retstr;
140+
if(1==numRows)
141+
return s;
142+
for(int i=0; i<numRows; ++i)
143+
{
144+
retstr.push_back(s[i]);
145+
int maxspan = 2*(numRows-1);
146+
int span1 = maxspan-i*2;
147+
int span2 = maxspan - span1;
148+
int cntpos = i;
149+
if(span1==0)
150+
span1 = span2;
151+
if(span2==0)
152+
span2 = span1;
153+
while(1)
154+
{
155+
if(cntpos+span1>=s.size())
156+
break;
157+
cntpos += span1;
158+
retstr.push_back(s[cntpos]);
159+
160+
if(cntpos+span2>=s.size())
161+
break;
162+
cntpos += span2;
163+
retstr.push_back(s[cntpos]);
164+
}
165+
}
166+
return retstr;
167+
}
168+
};
169+
```
170+
171+
### **C#**
172+
173+
```cs
174+
using System.Collections.Generic;
175+
using System.Linq;
176+
177+
public class Solution {
178+
public string Convert(string s, int numRows) {
179+
if (numRows == 1) return s;
180+
if (numRows > s.Length) numRows = s.Length;
181+
var rows = new List<char>[numRows];
182+
var i = 0;
183+
var j = 0;
184+
var down = true;
185+
while (i < s.Length)
186+
{
187+
if (rows[j] == null)
188+
{
189+
rows[j] = new List<char>();
190+
}
191+
rows[j].Add(s[i]);
192+
j = j + (down ? 1 : -1);
193+
if (j == numRows || j < 0)
194+
{
195+
down = !down;
196+
j = j + (down ? 2 : -2);
197+
}
198+
++i;
199+
}
200+
return new string(rows.SelectMany(row => row).ToArray());
201+
}
202+
}
203+
```
204+
205+
### **Go**
206+
207+
```go
208+
func convert(s string, numRows int) string {
209+
if numRows == 1 {
210+
return s
211+
}
212+
length := len(s)
213+
result := make([]byte, length)
214+
step := 2 * numRows - 2
215+
count := 0
216+
for i := 0; i < numRows; i++ {
217+
for j := 0; j + i < length; j += step {
218+
result[count] = s[i+j]
219+
count++
220+
if i != 0 && i != numRows - 1 && j + step - i < length {
221+
result[count] = s[j+step-i]
222+
count++
223+
}
224+
}
225+
}
226+
return string(result)
227+
}
228+
```
82229

230+
### **JavaScript**
231+
232+
```js
233+
/**
234+
* @param {string} s
235+
* @param {number} numRows
236+
* @return {string}
237+
*/
238+
var convert = function (s, numRows) {
239+
if (numRows == 1) return s;
240+
let arr = new Array(numRows);
241+
for (let i = 0; i < numRows; i++) arr[i] = [];
242+
let index = 0,
243+
len = s.length,
244+
mi = 0,
245+
isDown = true;
246+
while (index < len) {
247+
arr[mi].push(s[index]);
248+
index++;
249+
250+
if (mi >= numRows - 1) isDown = false;
251+
else if (mi <= 0) isDown = true;
252+
253+
if (isDown) mi++;
254+
else mi--;
255+
}
256+
let ans = [];
257+
for (let item of arr) {
258+
ans = ans.concat(item);
259+
}
260+
return ans.join('');
261+
};
262+
263+
const s = 'AB',
264+
numRows = 1;
265+
266+
console.log(convert(s, numRows));
83267
```
84268

85269
### **...**

0 commit comments

Comments
 (0)