Share my concise and simple C++ solution 16 ms


  • 0
    W
    string convert(string s, int numRows) {
            if(s.empty() || numRows < 2) return s;
            string result;
            for (int top=0; top<numRows; top++) {
                for (int left=top; left<s.size(); left += 2 * (numRows - 1)) {
                    result.push_back(s[left]);
                    if (top > 0 && top < numRows - 1 && left + 2 * (numRows - 1- top) < s.size()) {
                        result.push_back(s[left + 2 * (numRows - 1- top)]);
                    }
                }
            }
            return result;
        }
    

Log in to reply
 

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