6. ZigZag Conversion Go语言

go 语言

func convert(s string, numRows int) string {
    
    len_s := strings.Count(s,"") - 1
    
    var i,j int
    var str string = ""
    
    if numRows == 1 {
        return s
    }

    for i = 0; i < numRows; i++{
        if i == 0 || i == numRows - 1{
            for j = i;j < len_s; j += (numRows - 1) * 2{
                str += s[j:j+1]
            } 
        }else{
            var t int
            for j = i;j < len_s; j += (numRows - 1) * 2{
                str += s[j:j+1]
                t = j + (numRows - i - 1) * 2
                if t < len_s {
                    str += s[t:t+1]
                }
                
            }
        }
        
    }
    return str
}

 

点赞