Leetcode-6. Z 字形变换

6. Z 字形变换

思路:

设置一个方向指针,发现z规律

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Solution {
public:
string convert(string s, int numRows) {
if(numRows==1) return s;
vector<string> rows(min(int(s.length()),numRows));
int curRow=0;
bool go=false;
for(char c:s)
{
rows[curRow]+=c;
if(curRow==0||curRow==numRows-1) go=!go;
curRow+=(go)?1:-1;
}
string m;
for(string row:rows)
m+=row;
return m;
}
};