要求:下斜上
思路:巧用string数组vector模拟
class Solution {
public:
string convert(string s,int numRows){
if(numRows==1)return s;
vector<string> rows(min(numRows,int(s.size())));
int curRow=0;
bool goingDown=false;
for(char c:s){
rows[curRow]+=c;
if(curRow==0||curRow==numRows-1)goingDown=!goingDown;
curRow+=goingDown?1:-1;
}
string ret;
for(string row:rows)ret+=row;
return ret;
}
};
这篇博客介绍了一种使用C++编程的方法,通过vector和条件判断,实现将字符串按下斜上方向逐行填充,适用于解决字符串排列问题。作者提供了一个名为'Solution'的类和convert函数实例,展示了如何巧妙地模拟这一过程。
1524

被折叠的 条评论
为什么被折叠?



