My accepted js solution, any better code?


  • 0
    S

    Hi:
    the code as follows;
    Thanks for better advice.

    /**
     * @param {number} rowIndex
     * @return {number[]}
     */
    var getRow = function (rowIndex) {
    
        var tmp = [1, 1];
        var output = [1, 1];
    
    
        if (rowIndex == 0) {
            return [1];
        } else if (rowIndex == 1) {
            return [1, 1];
        }
    
        for (var i = 2; i <= rowIndex; i++) {
            for (var j = 1; j < tmp.length; j++) {
                var addCount = tmp[j - 1] + tmp[j];
                output.splice(j, 1, addCount);
            }
            output.push(1);
            tmp = output.slice(0, output.length);
        }
        return output;
    };
    

Log in to reply
 

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