Skip to content

Commit 857dc09

Browse files
author
Yeqi Tao
committed
Add soulution.go for 0006.ZigZag Conversion
1 parent 7e2e39b commit 857dc09

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
func convert(s string, numRows int) string {
2+
if numRows == 1 {
3+
return s
4+
}
5+
length := len(s)
6+
result := make([]byte, length)
7+
step := 2 * numRows - 2
8+
count := 0
9+
for i := 0; i < numRows; i++ {
10+
for j := 0; j + i < length; j += step {
11+
result[count] = s[i+j]
12+
count++
13+
if i != 0 && i != numRows - 1 && j + step - i < length {
14+
result[count] = s[j+step-i]
15+
count++
16+
}
17+
}
18+
}
19+
return string(result)
20+
}

solution/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
│   ├── README.md
4242
│   ├── Solution.cpp
4343
│   ├── Solution.java
44+
│   ├── Solution.go
4445
│   ├── Solution.js
4546
│   └── Solution.py
4647
├── 0007.Reverse Integer

0 commit comments

Comments
 (0)