Sharing my C++ solution


  • 1
    T
    class Solution {
    public:
        string convert(string s, int numRows) {
            if(numRows==1)
                return s;
                
            vector<string> temp(numRows, "");
            int n = s.length(), i;
            for(i=0; i<n; i++)
            {
                int module = i%(2*(numRows-1));
                if(module>=0 && module<numRows-1)
                    temp[module] += s[i];
                else
                    temp[2*(numRows-1)-module] += s[i];
            }
            
            string result;
            for(i=0; i<numRows; i++)
                result += temp[i];
                
            return result;
        }
    };

Log in to reply
 

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