领扣LintCode算法问题答案-1354. 杨辉三角形II

    科技2022-07-13  135

    领扣LintCode算法问题答案-1354. 杨辉三角形II

    目录

    1354. 杨辉三角形II描述样例 1:样例 2: 题解鸣谢

    1354. 杨辉三角形II

    描述

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

    注意行下标从 0 开始在杨辉三角中,每个数字是它上面两个数字的总和。

    样例 1:

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

    样例 2:

    输入: 4 输出: [1,4,6,4,1]

    题解

    public class Solution { /** * @param rowIndex: a non-negative index * @return: the kth index row of the Pascal's triangle */ public List<Integer> getRow(int rowIndex) { // write your code here int[] a = new int[rowIndex + 1]; a[0] = 1; int row = 1; while (row++ <= rowIndex) { a[row - 1] = 1; for (int i = row - 2; i >= 1; i--) { a[i] = a[i] + a[i - 1]; } } List<Integer> ret = new ArrayList<>(); for (int n : a) { ret.add(n); } return ret; } }

    原题链接点这里

    鸣谢

    非常感谢你愿意花时间阅读本文章,本人水平有限,如果有什么说的不对的地方,请指正。 欢迎各位留言讨论,希望小伙伴们都能每天进步一点点。

    Processed: 0.010, SQL: 8