题目要求
- 1 <= s.length <= 1000
- s 由英文字母(小写和大写)、’,’ 和 ‘.’ 组成
- 1 <= numRows <= 1000
代码
class Solution {
public:
string convert(string s, int numRows) {
if (numRows == 1)
{
return s;
}
int interval = numRows - 2;
vector<string> strV(numRows);
for (size_t i = 0; i < s.length(); i++)
{
int index = i % (numRows + interval);
if (index < numRows)
{
strV[index] += s[i];
}
else
{
index = index - numRows;
strV[strV.size() - index - 2] += s[i];
}
}
for (auto i : strV)
cout << i << ends;
string resultStr;
for (auto i : strV)
resultStr += i;
return resultStr;
}
};
整体思路
这道题最主要的是观察字符串和变型模式,找到模式的规律后,剩下的就迎刃而解了。
给了一个字符串和一个行数。
字符串里面的字符按照组来划分。每组有numRows+(numRows - 2)个字符。
每一组中,前numRows个按照正常顺序放进对应编号的容器中。后面numRows-2个字符是Z字形两个竖直之间的斜线。从倒数第二行开始倒序往前排。
学到了什么
1、字符串的处理…