美文网首页
119. 杨辉三角 II

119. 杨辉三角 II

作者: 吃饭用盘装 | 来源:发表于2018-06-08 23:46 被阅读8次

内容

给定一个非负索引 k,其中 k ≤ 33,返回杨辉三角的第 *k *行。

image

<small style="box-sizing: border-box; font-size: 12px;">在杨辉三角中,每个数是它左上方和右上方的数的和。</small>

示例:

<pre style="box-sizing: border-box; overflow: auto; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 13px; display: block; padding: 9.5px; margin: 0px 0px 10px; line-height: 1.42857; color: rgb(51, 51, 51); word-break: break-all; word-wrap: break-word; background-color: rgb(245, 245, 245); border: 1px solid rgb(204, 204, 204); border-radius: 4px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">输入: 3
输出: [1,3,3,1]
</pre>

进阶:

你可以优化你的算法到 O(k) 空间复杂度吗?


思路


代码

/**
 * @param {number} rowIndex
 * @return {number[]}
 */
var getRow = function (rowIndex) {
    if (rowIndex == 0) return [1];
    if (rowIndex == 1) return [1, 1];
    if (rowIndex == 2) return [1, 2, 1];
    var save = [1, 2, 1];

    for (var i = 3; i <= rowIndex; i++) {
        var newArray = [];
        for (var j = 0; j < save.length - 1; j++) {
            newArray.push(save[j] + save[j + 1]);
        }

        newArray.unshift(1);
        newArray.push(1);

        save = newArray;
    }

    return save;
};

回到目录

相关文章

  • Leetcode-119 杨辉三角 II

    119. 杨辉三角 II[https://leetcode-cn.com/problems/pascals-tri...

  • 2021.2.12每日一题

    119. 杨辉三角 II[https://leetcode-cn.com/problems/pascals-tri...

  • 119. 杨辉三角 II

    leetcode 119. 杨辉三角 II 题目 给定一个非负索引 k,其中 k ≤ 33,返回杨辉三角的第 k ...

  • [数组]杨辉三角 II

    119. 杨辉三角 II 题目描述 给定一个非负索引 k,其中 k ≤ 33,返回杨辉三角的第 k 行。 示例:输...

  • 力扣随机解题

    118. 杨辉三角 119. 杨辉三角 II 94. 二叉树的中序遍历 704. 二分查找 21. 合并两个有序链...

  • python实现leetcode之119. 杨辉三角 II

    解题思路 思路与上一题一样,保留最后一行 119. 杨辉三角 II[https://leetcode-cn.com...

  • 119. 杨辉三角 II

    内容 给定一个非负索引 k,其中 k ≤ 33,返回杨辉三角的第 *k *行。 在杨辉三角中,每个数是它左上方和右...

  • 119. 杨辉三角 II

    【问题描述】给定一个非负索引 k,其中 k ≤ 33,返回杨辉三角的第 k 行。在杨辉三角中,每个数是它左上方和右...

  • 119.杨辉三角II

    题目描述 给定一个非负索引 k,其中 k ≤ 33,返回杨辉三角的第 k 行。 示例: 在杨辉三角中,每个数是它左...

  • 119. 杨辉三角 II

    题目描述 给定一个非负索引 k,其中 k ≤ 33,返回杨辉三角的第 k 行。 在杨辉三角中,每个数是它左上方和右...

网友评论

      本文标题:119. 杨辉三角 II

      本文链接:https://www.haomeiwen.com/subject/ygpzsftx.html