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

Pascal's Triangle II

时间:2014-09-09 12:45:05      阅读:118      评论:0      收藏:0      [点我收藏+]

标签:算法   leetcode   algorithm   

Given an index k, return the kth row of the Pascal‘s triangle.

For example, given k = 3,
Return [1,3,3,1].

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

答案

public class Solution {
    public List<Integer> getRow(int rowIndex) {
        ArrayList<Integer> p=new ArrayList<Integer>();
        if(rowIndex<0)
        {
            return p;
        }
        int array[]=new int[rowIndex+1];
        int i,j;
        for(i=0;i<=rowIndex;i++)
        {
            array[i]=1;
        }
        for(i=2;i<=rowIndex;i++)
        {
            for(j=i-1;j>0;j--){
                array[j]+=array[j-1];
            }
        }
        for(int value:array){
            p.add(value);
        }
        return p;
    }
}



Pascal's Triangle II

标签:算法   leetcode   algorithm   

原文地址:http://blog.csdn.net/jiewuyou/article/details/39152367

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