-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathL6.go
More file actions
117 lines (107 loc) · 1.69 KB
/
L6.go
File metadata and controls
117 lines (107 loc) · 1.69 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package main
import "fmt"
func main() {
res := convert3("PAYPALISHIRING", 3)
fmt.Println(res)
}
func convert(s string, numRows int) string {
if numRows == 1 {
return s
}
length := len(s)
sub := 2*numRows - 2
eachL := numRows - 1
a1 := length / sub
a2 := length % sub
extr := 0
if a2 <= numRows && a2 > 0 {
extr = 1
} else if a2 > numRows {
extr = a2 - numRows + 1
}
col := a1*eachL + extr
mem := make([][]string, numRows)
for i := 0; i < numRows; i++ {
mem[i] = make([]string, col)
}
x := 0
y := 0
down := true
for i := 0; i < length; i++ {
mem[x][y] = s[i : i+1]
if x == 0 {
down = true
}
if x == numRows-1 {
down = false
}
if down {
x++
} else {
x--
y++
}
}
var res string
for _, row := range mem {
for _, str := range row {
res += str
}
}
return res
}
func convert2(s string, numRows int) string {
if numRows == 1 {
return s
}
length := len(s)
mem := make([][]string, numRows)
for i := 0; i < numRows; i++ {
mem[i] = make([]string, 0)
}
x := 0
y := 0
down := true
for i := 0; i < length; i++ {
mem[x] = append(mem[x], s[i:i+1])
if x == 0 {
down = true
}
if x == numRows-1 {
down = false
}
if down {
x++
} else {
x--
y++
}
}
var res string
for _, row := range mem {
for _, str := range row {
res += str
}
}
return res
}
func convert3(s string, numRows int) string {
n := len(s)
if numRows < 2 || numRows > n {
return s
}
t := 2*numRows - 2
res := make([]byte, n)
idx := 0
for i := 0; i < numRows; i++ {
for j := 0; j+i < n; j += t {
res[idx] = s[i+j]
if i > 0 && i < numRows-1 && j+t-i < n {
idx++
res[idx] = s[j+t-i]
}
idx++
}
}
return string(res)
}