Simple js solution using properties of pascal's triangles


  • 0
    D

    So, in Pascal's triangles, the number in (#row, #column) is the binomial coefficient of C(#row, #column)

    Therefore, if given row, col, the number(row, col) can be calculated in the following way

    previousNumber = number(row, col - 1)
    number(row, col - 1) = previousNumber * (row - col + 1) / col
    

    A javascript implementation is like this

    var getRow = function(rowIndex) {
        var currentRow = new Array(rowIndex + 1).fill(1)
        for (i = 1; i <= rowIndex / 2; i ++) {
            const currentCol = (currentRow[i - 1] * (rowIndex - i + 1)) / i
            currentRow[i] = currentRow[rowIndex - i] = currentCol
        }
        return currentRow
    }
    

Log in to reply
 

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