Very Simple solution in C++


  • 0
    M

    class Solution {
    public:
    string convert(string s, int numRows) {

        vector<string> level(numRows);
        
        const int total = 2*numRows - 2; 
        
        for(unsigned i=0; i<s.length();++i){
            int place = (total > 0) ? i%total:0;
            place = (place<numRows)?place:total-place;
            level[place] += s[i];
            }
            
            string result;
            
            for(auto str:level)
                result += str;
        return result;
    }
    

    };


Log in to reply
 

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