My accepted 16ms c++ solution


  • 0
    W
    class Solution {
    public:
        string convert(string s, int numRows) {
            if (numRows == 1 || s.length() <= numRows) return s;
            string res;
            for (int i = 0; i < numRows; ++i)
            {
                for (int j = 0; i+j*2*(numRows-1) < s.length(); ++j)
                {
                    res += s[i+j*2*(numRows-1)];
                    if (i != 0 && i != numRows-1 && i+2*(numRows-i-1)+j*2*(numRows-1) < s.length()) res += s[i+2*(numRows-i-1)+j*2*(numRows-1)];
                }
            }
            return res;
        }
    };

Log in to reply
 

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