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;
    }
};