6.Z字形变换
题目描述:将一个给定字符串s根据给定的行数numRows,从上往下、从左到右进行Z字形排列
示例:
输入:s = "PAYPALISHIRING", numRows = 4
输出:"PINALSIGYAHRPI"
解释:
P I N
A L S I G
Y A H R
P I
输入:s = "A", numRows = 1
输出:"A"
思路:
用vector<string>newStr,字符串数组作为新数组,遍历s,加到对应行的字符串后面。
遍历思路:根据up(路线方向)看current是++还是--,如果current到最上或最下了,up转向
代码+解析:
class Solution {
public:
string convert(string s, int numRows) {
int len = s.size();
if(len == 1 || numRows == 1 || len <= numRows){ //只有1个的情况
return s;
}
vector<string> newStr(numRows);
int current = 0; //当前行
int up = 0; //up=0表示向