12 lines concise and easy understand C++ solution


  • 3
    A
    class Solution {
    public:
        string convert(string s, int numRows) {
            string strs[numRows];
            if(numRows <= 1) return s;
            int row = 0, step = 1;
            for(int i = 0 ; i < s.size(); i++){
                strs[row].push_back(s[i]);
                if(row == numRows - 1) step = -1;
                if(row == 0) step = 1;
                row += step;
            }
            string res = "";
            for(auto str : strs) res = res + str;
            return res;
        }
    };

Log in to reply
 

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