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));

1 comment:

  1. Custom Optical Services - With the rapid development of the market, customers' requirements for optical products are more diversified, and new equipment and instruments require special optical components. The experts and team of Alfa Chemistry have been engaged in optical manufacturing for a long time, with many years of optical manufacturing experience, matched with advanced optical manufacturing design and technology, and can perform cost analysis for any optical size.

    ReplyDelete

Blogroll

Popular Posts