Given an index k, return the kth row of the Pascal's triangle.
For example, given k = 3,
Return [1,3,3,1]
.
思路:我们可以把这个pascal三角用二维数组来解决。
a[i][0]=1,a[i][j]=a[i-1][j-1]+a[i-1][j]
根据此递推式就可以得出a[introws][j]了
代码如下(已通过leetcode)
public class Solution {
public List<Integer> getRow(int rowIndex) {
List<Integer> list=new ArrayList<Integer>();
int[][] a= new int[rowIndex+1][rowIndex+1];
for(int i=0;i<rowIndex+1;i++)
a[i][0]=1;
for(int i=1;i<rowIndex+1;i++) {
for(int j=1;j<rowIndex+1;j++)
a[i][j]=a[i-1][j-1]+a[i-1][j];
}
for(int i=0;i<rowIndex+1;i++)
list.add(a[rowIndex][i]);
return list;
}
}