# leetcode【第七周】Z字形排序字符串

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 text, int nRows);`

`convert("PAYPALISHIRING", 3)`
should return
`"PAHNAPLSIIGYIR"。`

``````class Solution {
public:
string convert(string s, int numRows) {
if(numRows==1)
return s;
vector contain;
int len = s.length();
for(int i=0;i``````

``````class Solution {
public:
string convert(string s, int numRows) {
if(numRows == 1) return s;
string tmp[numRows];
int i = 0, j, inclined = numRows-2;
while(i < s.size()){
for(j = 0; i < s.size() && j < numRows; ++j) tmp[j] += s[i++];
for(j = inclined; i < s.size() && j > 0; --j) tmp[j] += s[i++];
}
string result = "";
for(i = 0; i < numRows; ++i)
result += tmp[i];
return result;
}
};``````

原文作者：Z字形编排问题
原文地址: https://blog.csdn.net/PengXR2017/article/details/69939052
本文转自网络文章，转载此文章仅为分享知识，如有侵权，请联系博主进行删除。