76% javascript code, not bad


  • 0
    E

    outside loop - numRows loop
    inside loop - string length loop
    first line and last line - concat one char per time inside loop
    middle lines - concat two chars per time inside loop except string length exceed. child gap = (numRows-i)*2-1.

    /**
     * @param {string} s
     * @param {number} numRows
     * @return {string}
     */
    var convert = function(s, numRows) {
        if (numRows===0 || numRows===1){return s;}
        var gap = (numRows-1)*2-1;
        var gap1 = 0;
        var i=1;j=0;
        var result="";
        for (;i<=numRows;i++){
            for (j=i-1;j<=s.length-1;j++){
                //console.log("i=",i,"j=",j);
                result+=s.charAt(j);
                if (1<i && i<numRows){
                    gap1 = (numRows-i)*2-1;
                    if(j+gap1+1<=s.length-1) {result+=s.charAt(j+gap1+1);}
                }
                j+=gap;
                //console.log(result);
            }
        }
        return result;
    };
    

Log in to reply
 

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