-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
68 lines (64 loc) · 1.23 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package main
import (
"fmt"
)
//func convert(s string, numRows int) string {
// var res string
// if numRows == 1 {
// return s
// }
// for i := 0; i < numRows; i++ {
// j := i
// for j < len(s) {
// res += s[j : j+1]
// if i != 0 && i != numRows-1 && j+(numRows-i)*2-2 < len(s) {
// res += s[j+(numRows-i)*2-2 : j+(numRows-i)*2-1]
// }
// j += (numRows - 1) * 2
// }
// }
// return res
//}
func convert(s string, numRows int) string {
fmt.Printf("s: %v\n", s)
fmt.Printf("numRowss: %v\n", numRows)
length := len(s)
result := make([]byte, length)
side := numRows*2 - 2
fmt.Printf("side: %v\n", side)
if numRows == 1 {
return s
}
index := 0
for i := 0; i < numRows; i++ {
fmt.Printf("-----%v-----\n", i)
j := i
if i == 0 || i == numRows-1 {
for j < length {
fmt.Printf("index: %v\n", index)
fmt.Printf("j: %v\n", j)
result[index] = s[j]
index++
j += side
}
} else {
top := true
for j < length {
fmt.Printf("index: %v\n", index)
fmt.Printf("j: %v\n", j)
result[index] = s[j]
index++
if top {
j += side - i*2
} else {
j += i * 2
}
top = !top
}
}
}
return string(result)
}
func main() {
fmt.Println(convert("abcdefghijklmnopqrstuvwxyz", 5))
}