# 【LeetCode】Z字形变换（Zigzag Conversion）

###### 题目：

The string “PAYPALISHIRING” is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)
P       A        H       N
A   P  L   S   I    I   G
Y       I         R
And then read line by line: “PAHNAPLSIIGYIR”
Write the code that will take a string and make this conversion given a number of rows:
string convert(string s, int numRows);

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

string convert(string s, int numRows)

###### 代码:
``````class Solution {
public static String convert(String s, int numRows) {
if(numRows == 1)
return s;
int length = s.length();
int circle, size;
circle = numRows * 2 - 2;
size = (length + circle - 1) / circle;
StringBuilder string = new StringBuilder();
for (int i = 0; i < numRows; i++) {
for (int j = 0; j < size; j++) {
if (circle * j + i < length)
string.append(s.charAt(circle * j + i));
if (circle * j + circle - i < length && i != numRows - 1 && i != 0)
string.append(s.charAt(circle * j + +circle - i));
}
}
return new String(string);
}
}
``````
原文作者：Z字形编排问题
原文地址: https://blog.csdn.net/sup_chao/article/details/84845195
本文转自网络文章，转载此文章仅为分享知识，如有侵权，请联系博主进行删除。