easy C++ solution use 15ms


  • 0
    G
    #include <string>
    using namespace std;
    
    class Solution {
    public:
        string convert(string s, int rows) {
            int n = s.length();
    
            if (rows <= 1 || rows >= n)
                return s;
    
            string ret;
            ret.reserve(n);
    
            for (int i = 0; i < rows; ++i) {
                int j = i;
                while (j < n)
                {
                    ret.push_back(s[j]);
                    j += 2 * (rows - j % (rows - 1) - 1);
                }
            }
    
            return ret;
        }
    };
    

Log in to reply
 

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