javascript simple solution: o(n) time, o(n) space


  • 1
    L
    var convert = function(s, numRows) {
        var len = s.length;
        if (numRows < 2 || numRows >= len)
            return s;
        
        var rowString = [], 
        	rowIndex = 0, 
        	upDownFlag = 1, 
        	i = 0;
    
        for(;i<numRows;i++)
        	rowString[i] = "";
    
        for (i = 0; i < len; ++i) {
            rowString[rowIndex] += s[i];
            if (rowIndex == numRows-1)
                flag = -1;
            else if (rowIndex === 0) 
                flag = 1;
    
            rowIndex += flag;
        }
        return rowString.join("");
    };
    

Log in to reply
 

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