Clean c++ solution


  • 0

    build the result row by row

    string convert(string s, int numRows) {
        int size = s.size(), dist = numRows == 1 ? 1 : numRows * 2 - 2;
        string result = "";
        for (int i = 0; i < numRows; i++) {
            int gap = dist - 2 * i, current = i;
            while (current < size) {
                result += s[current];
                if (!(i == 0 || i == numRows - 1)) {
                    if (current + gap < size)
                        result += s[current + gap];
                }
                current += dist;
            }
        }
        return result;
    }
    

Log in to reply
 

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