> 簡潔易懂講清原理,講不清你來打我~
輸入字串,按下右上下右上排列後輸出字串


> 模擬
簡單的思路
利用字串陣列模擬Z字形,從左到右遍歷按下=》右上=》下=》右上填充字串陣列,最後按行輸出Z字形字串陣列
精準的定義
Z是要填充的字串陣列
i是要填充的字元下標s
down是向下標誌
row是字元要填充在哪一行
ans是按行輸出Z的結果
```cpp
class Solution {
public:
string convert(string s, int numRows) {
if(numRows==1)return s;
vector<string>Z(min(numRows,(int)s.size()));//Z是要填充的字串陣列
bool down =true;//down是向下標誌
int row=0;//row是字元要填充在哪一行
string ans;//ans是按行輸出Z的結果
for(int i=0;i<s.size();i++){
//i是要填充的字元的下標
Z[row]+=s[i];
//邊界
if(row==numRows-1&&down||row==0&&!down){
down=!down;
}
row+=down?1:-1;
}
//按行輸出Z的結果
for(auto a:Z){
ans+=a;
}
return ans;
}
};
```
踩過的坑
```cpp
if(numRows==1)return s;
min(numRows,(int)s.size())
```
> 喜歡簡潔易懂還能講清楚原理部落格的小夥伴就關注關注這個非常高產的博主呀,下次再會~