C++ Simple Solution


  • 1

    I think this question deserved as a Medium Question.

    class Solution {
    public:
        string convert(string s, int numRows) {
            if (numRows == 1)
                return s;
            string res;
            int n = s.size();
            for (int i = 0; i < numRows; i++) {
                int index = i, dis1 = (numRows - i - 1) * 2, dis2 = (i) * 2;
                if (i == 0)
                    dis2 = dis1;
                else if (i == numRows - 1)
                    dis1 = dis2;
                bool f = true;
                while (index < n) {
                    res.push_back(s[index]);
                    index += f ? dis1 : dis2;
                    f = !f;
                }
            }
            return res;
        }
    };
    

Log in to reply
 

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