leetcode--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 {
    /**This problem is an implementation of dynamic programming methodology.
     * @author Averill Zheng
     * @version 2016-06-05
     * @since JDK 1.7
     */ 
    public List<Integer> getRow(int rowIndex) {
        List<Integer> nthRow = new ArrayList<Integer>();
		nthRow.add(1);
		if(rowIndex > 0){   
		    nthRow.add(1);
		    for(int i = 2; i <= rowIndex; ++i){
			    nthRow.add(0, 1);
			    for(int j = 1; j < i; ++j)
				    nthRow.set(j, nthRow.get(j) + nthRow.get(j + 1));
			}
		}
		return nthRow;     
    }
}

  

posted @ 2014-06-07 01:45  Averill Zheng  阅读(165)  评论(0编辑  收藏  举报