My c++ solution


  • 0
    S
    string convert(string s, int numRows) {
        if (numRows < 2 || s.size() < 2) return s;
        string str;
        for (int i = 0; i < numRows; i++){
            int step = 2 * (numRows - 1);
            for (int j = 0; j < s.size() + step; j += step){
                if (j - i >= 0 && j - i < s.size())
                    str += s[j - i];
                if (i == 0 || i == numRows - 1) continue;    //avoid double counting in the boundary.
                if (j + i < s.size())
                    str += s[j + i];
            }
        }
        return str;
    }

Log in to reply
 

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