【JS每日一算法】🟩113.杨辉三角II(模拟法、模拟法优化、数学公式)

328 阅读1分钟

给定一个非负索引 rowIndex,返回「杨辉三角」的第 rowIndex 行。

在「杨辉三角」中,每个数是它左上方和右上方的数的和。

提示:

  • 0 <= rowIndex <= 33

b70b953a4dc245cfb8fa5bdbe438c30f~tplv-k3u1fbpfcp-jj-mark_3024_0_0_0_q75.awebp

示例 1:

输入: rowIndex = 3
输出: [1,3,3,1]

示例 2:

输入: rowIndex = 0
输出: [1]

题解:

个人博客

更多JS版本题解点击链接关注该仓库👀

/**
 * @description: 模拟法  TC:O(n^2)  SC:O(n^2)
 * @author: JunLiangWang
 * @param {*} rowIndex 给定杨辉三角形行索引
 * @return {*}
 */
function simulation(rowIndex){
    /**
     * 本方案利用迭代模拟杨辉三角形生成过程
     * 然后取出最后一行即可
     */
    let outArray=[];
    for(let i=0;i<=rowIndex;i++){
        let tempArray=new Array(i+1).fill(1);
        for(let j=1;j<i;j++){
            tempArray[j]=outArray[i-1][j-1]+outArray[i-1][j]
        }
        outArray.push(tempArray)
    }
    return outArray[outArray.length-1]
}


/**
 * @description: 模拟法优化  TC:O(n^2)  SC:O(n)
 * @author: JunLiangWang
 * @param {*} rowIndex 给定杨辉三角形行索引
 * @return {*}
 */
function simulationOp(rowIndex){
    /**
     * 上述模拟法我们发现,其实我们当前行仅与上一行有关
     * 因此我们可以利用一维滚动数组对空间复杂度进一步优
     * 化
     */
    let outArray=new Array(rowIndex+1).fill(1);
    for(let i=0;i<=rowIndex;i++){
        for(let j=i-1;j>=1;j--)
              outArray[j]=outArray[j]+outArray[j-1]
    }
    return outArray
}


/**
 * @description: 数学方法  TC:O(n)  SC:O(n)
 * @author: JunLiangWang
 * @param {*} rowIndex 给定杨辉三角形行索引
 * @return {*}
 */
function Math(rowIndex){
    /**
     * 对于杨辉三角形i行i列有公式
     * f[i][j]=f[i][j-1]*(rowsCount-j)/j
     */
    let outArray=[1];
    for(let i=1;i<=rowIndex;i++){
        outArray.push(outArray[i - 1]* (rowIndex - i + 1) / i);
    }
    return outArray
}