Java [Leetcode 119]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].

解题思路:

每次在上一个list前面插入1,然后后面的每两个间相加赋值给前一个数。

代码描述:

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

  

posted @ 2016-01-27 21:09  scottwang  阅读(290)  评论(0编辑  收藏  举报