[LeetCode]6. ZigZag Conversion

版权声明:长风原创 https://blog.csdn.net/u012846486/article/details/87430957

热身练手题

func convert(s string, numRows int) string {
    if numRows <= 1 || len(s) <= numRows { return s }
    
    var result []byte // 字符串拼接
    
    var kGroupSize = numRows * 2 - 2
    len := len(s)
    for i := 0; i < numRows; i++ {
        for j := 0; j < len && j+i < len; j += kGroupSize {
            result = append(result, s[j+i])
            oft := kGroupSize - i
            if oft < kGroupSize && oft != i && j+oft<len {
                result = append(result, s[j+oft])
            }
        }
    }
    
    return string(result)
}

猜你喜欢

转载自blog.csdn.net/u012846486/article/details/87430957