C++ accepted solution


  • 1
    X
    // leetcode zigzag conversion
    class Solution {
    public:
        string convert(string s, int numRows) {
            if (numRows == 1) return s;
            string des[numRows];
            int row = 0;
            int step = 1;
            for (auto c: s) {
                des[row] += c;
                if (row == numRows - 1) step = -1;
                if (row == 0) step = 1;
                row += step;
            }
            string res;
            for (int i = 0; i < numRows; i++) {
                res += des[i];
            }
            return res;
        }
    };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.