首页 > 其他 > 详细

125.Pascal's Triangle II

时间:2018-07-20 22:05:52      阅读:147      评论:0      收藏:0      [点我收藏+]

题目:

Given a non-negative index k where k ≤ 33, return the kth index row of the Pascal‘s triangle.

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

Note that the row index starts from 0.

请注意,行索引从0开始。

技术分享图片
In Pascal‘s triangle, each number is the sum of the two numbers directly above it.

在Pascal的三角形中,每个数字是它上面两个数字的总和。

Example:

Input: 3
Output: [1,3,3,1]

Follow up:

Could you optimize your algorithm to use only O(k) extra space?

你能优化算法只使用O(k)额外空间吗?

解答:

class Solution {
    public List<Integer> getRow(int rowIndex) {
        List<Integer> list=new ArrayList<>();
        if(rowIndex<0){
            return list;
        }
        for(int i=0;i<rowIndex+1;i++){
            list.add(0,1);
            for(int j=1;j<list.size()-1;j++){
                list.set(j,list.get(j)+list.get(j+1));
            }
        }
        return list;
    }
}

详解:

 

125.Pascal's Triangle II

原文:https://www.cnblogs.com/chanaichao/p/9257281.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!