# LeetCode 刷题笔记 - 6. Z 字形变换

## 描述：

``````L   C   I   R
E T O E S I I G
E   D   H   N

## 示例

``````1:

``````2:

L     D     R
E   O E   I I
E C   I H   N
T     S     G

swift

## 解析：

``````A       B       C
A     A B     B C     C
A   A   B   B   C   C
A A     B B     C C
A       B       C

``````0       8         16
1     7 9      15 17     23
2   6   10   14   18   22
3 5     11 13     19 21
4       12        20

``````class Solution {
func convert(_ s: String, _ numRows: Int) -> String {
if numRows == 1 {
return s
}
var resultString = ""
let stringArray = Array(s)
let offset = numRows * 2 - 2
let count = stringArray.count
let groupNumber = count / offset + 1
for row in 0...numRows - 1 {
for groupIndex in 0...groupNumber - 1 {
if (groupIndex * offset  + row) < count {
resultString = resultString + String(stringArray[groupIndex * offset  + row])
}
if row != 0 && row != numRows - 1 {
if (groupIndex + 1) * offset - row < count {
resultString = resultString + String(stringArray[(groupIndex + 1) * offset - row])
}
}
}
}
return resultString
}
}