Swift 4.2
class Solution {
func convert(_ s: String, _ numRows: Int) -> String {
guard numRows > 1 else { return s }
var characters = Array(s)
var str = ""
for row in 0...numRows - 1 {
var i = 0
var index = 2 * i * (numRows - 1) + row
while index < characters.count {
str.append(characters[index])
i += 1
let leftIndex = 2 * i * (numRows - 1) - row
index = 2 * i * (numRows - 1) + row
if row > 0 {
if leftIndex < characters.count, row != (numRows - 1) {
str.append(characters[leftIndex])
}
}
}
}
return str
}
}
网友评论