Simple C++ solution [AC]


  • 0
    class Solution {
    public:
        string convert(string s, int numRows) {
            int n = numRows;
            if(n==1)
                return s;
            string res[n];
            for(int i=0, row =0, dir =0;i<s.length();i++)
            {
                res[row].push_back(s[i]);
                if(row==n-1 || row==0)
                    dir = 1-dir;
                dir?row++:row--;
            }
            string finalres;
            for(int i=0;i<n;i++)
                finalres+=res[i];
            return finalres;
        }
    };
    

Log in to reply
 

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