2075. 解码斜向换位密码

69 阅读1分钟

题目:
leetcode.cn/problems/de…
算法:
方法一:模拟题

func decodeCiphertext(encodedText string, rows int) string {
    n := len(encodedText)
    cols := n / rows
    ans := []byte{}
    lastNotSpace := 0
    for j := 0; j < cols; j ++ {
        x, y := 0, j
        pos := j
        for x < rows && y < cols {
            ans = append(ans, encodedText[pos])
            if string(encodedText[pos]) != " " {
                lastNotSpace = len(ans)
            }
            x ++
            y ++
            pos = pos + cols + 1
            
        }
    }
    ans = ans[:lastNotSpace]
    return string(ans)
}