N 字形变换 - C++力扣6题
题目链接:https://leetcode.com/problems/zigzag-conversion/description/
解题思路
这题就是纯粹的模拟题,难度算是中等偏下吧,这题其实也没什么好说的,就是把上下的方向搞懂了就行,直接上代码吧。
完整代码:
class Solution {
public:
    string convert(string s, int numRows) {
        if(numRows <= 1) return s;
        bool down = false;
        vector<string> r(numRows + 1);
        int curRow = 0;
        for(auto c : s) {
            r[curRow] += c;
            if(curRow == 0 || curRow == numRows - 1) down = !down;
            curRow += down ? 1 : -1;
        }
        string ret;
        for(auto rs : r) ret += rs;
        return ret;
    }
};
版权属于:我是苏云曦吖
本文链接:https://yuisblog.com/archives/227/
本站未注明转载的文章均为原创,并采用 
    CC BY-NC-SA 4.0 授权协议,转载请注明来源,谢谢!
     
 
 
     
  
     
         