首页 > 代码库 > Pascal's Triangle II

Pascal's Triangle II

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