# 【LeetCode】 6. ZigZag Conversion
## Description
> 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);`
## Example:
```
Example 1:
Input: s = "PAYPALISHIRING", numRows = 3
Output: "PAHNAPLSIIGYIR"
Example 2:
Input: s = "PAYPALISHIRING", numRows = 4
Output: "PINALSIGYAHRPI"
Explanation:
P I N
A L S I G
Y A H R
P I
```
## Solution
* 用一個flag記錄現在往下還是往上。
* 用一個count記錄現在要丟在哪一行,根據flag每次++或--。
### Code
```C++=1
class Solution {
public:
string convert(string s, int numRows) {
if(numRows==1)return s;
string *v =new string[numRows];
for(int i=0,flag=0,count=0;i<s.length();i++)
{
v[count].push_back(s[i]);
if(flag==0)
{
if(count==numRows-1)
{
count--;
flag=1;
}
else
count++;
}
else
{
if(count==0)
{
count++;
flag=0;
}
else
count--;
}
}
string ans;
for(int i=0;i<numRows;i++)
ans+=v[i];
return ans;
}
};
```
###### tags: `LeetCode` `C++`