Problem

Given an index k, return the kth row of the Pascal's triangle. 

Example

when k = 3, the row is [1,3,3,1].

JavaScript Code

function getRow(rowIndex) {
    var result = [];
 
    if (rowIndex < 0)
        return result;
 
    result.push(1);
    for (var i = 1; i <= rowIndex; i++) {
        for (var j = result.length - 2; j >= 0; j--) {
            result[j + 1] = result[j] + result[j + 1];
        }
        result.push(1);
    }
    return result;
}

console.log(getRow(6));

0 comments:

Blogroll

Follow this blog by Email

Popular Posts