my simple C++ solution


  • 0
    W
    class Solution {
    public:
        string convert(string s, int numRows) {
            int slen = s.size();
            if(numRows>=slen || numRows==1) return s;
            string ret;
            for (int i = 0; i < numRows; i++) {
                for (int j = 0;; j++) {
                    if (2 * j * (numRows - 1) + i >= slen) break;
                    else {
                        ret += s[2 * j * (numRows - 1) + i];
                        if(i==0 || i==numRows - 1) continue;
                        if (2 * j * (numRows - 1) + i + 2 * (numRows - 1 - i) >= slen) break;
                        else {
                            ret += s[2 * j * (numRows - 1) + i + 2 * (numRows - 1 - i)];
                        }
                    }
                }
            }
            return ret;
        }
    };
    

Log in to reply
 

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