Accepted Solution with 16ms


  • 0
    J
    class Solution {
    public:
        string convert(string s, int numRows) {
            int size = s.size();
            if (numRows >= size || numRows <= 1) return s;
            string z;
            for (int i = 0; i < numRows; i++)
            {
                int idx = i;
                while (idx < size)
                {
                    if (i < numRows-1)
                    {
                        z.push_back(s.at(idx));
                        idx += 2*(numRows-i-1);
                    }
                    if (i > 0 && idx < size)
                    {
                        z.push_back(s.at(idx));
                        idx += 2*i;
                    }
                }
            }
            return z;
        }
    };

Log in to reply
 

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