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
// @Title: Z 字形变换 (ZigZag Conversion)
// @Author: 15816537946@163.com
// @Date: 2022-03-01 19:52:55
// @Runtime: 8 ms
// @Memory: 5.4 MB
func convert(s string, numRows int) string {
    if numRows == 1 {
        return s
    }

    rows := make([]string, numRows)
    n := 2 *numRows -2 
    for i, char := range s {
        x := i %n 
        rows[min(x, n-x)]+= string(char)
    }

    return strings.Join(rows, "")
}

func min(a,b int) int {
    if a < b {
        return a
    }
    return b
}