Java for 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]
.
解题思路:
注意,本题的k相当于上题的k+1,其他照搬即可,JAVA实现如下:
public List<Integer> getRow(int rowIndex) { List<Integer> alist=new ArrayList<Integer>(); rowIndex++; if(rowIndex<=0) return alist; alist.add(1); for(int i=2;i<=rowIndex;i++){ List<Integer> alist2=new ArrayList<Integer>(); alist2.add(1); for(int j=1;j<i-1;j++){ alist2.add(alist.get(j-1)+alist.get(j)); } alist2.add(1); alist=alist2; } return alist; }