class Solution {
public:
string convert(string s,int numRows)
{
if (numRows == 1)
{
return s;
}
int sSize = s.size();
int storeSize = min(sSize, numRows);
vector<string> store(storeSize, "");//初始化方法 确定行数来resize了vector
int loc = 0;
string result;
bool change = false;
for (int i = 0; i < sSize; i++)
{
store[loc].push_back(s[i]);
if (loc == 0 || loc == numRows - 1)
{
change = !change;//改变方向,在最后一行和第一行需要改变方向
}
loc += change ? 1 : -1;
}
for (int index = 0; index < storeSize; index++)
{
result = result + store[index];
}
return result;
}
};
力扣6
最新推荐文章于 2022-01-27 23:58:32 发布