C++ solution (simple)


  • 0
    H
    class Solution {
    public:
        string convert(string s, int numRows) {
         
            // trival case
            if(numRows==1)
                return s;
    
            int interval=(numRows-1)*2;
            string output;
            for(int i=0 ; i<numRows ; i++) {
                for(int j=0 ; j<s.size() ; j++) {
                    if((j%interval)==i || (j%interval)==2*(numRows-1)-i)
                        output+=s.at(j);
                }
            }
            return output;        
        }
    };
    '''

Log in to reply
 

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