码迷,mamicode.com
首页 > 其他好文 > 详细

125.Pascal's Triangle II

时间:2018-07-20 22:31:37      阅读:175      评论:0      收藏:0      [点我收藏+]

标签:algo   img   art   common   list   array   animate   rect   pac   

题目:

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

标签:algo   img   art   common   list   array   animate   rect   pac   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!