給定一個非負索引?rowIndex
,返回「楊輝三角」的第?rowIndex
?行。
在「楊輝三角」中,每個數是它左上方和右上方的數的和。
示例 1:
輸入: rowIndex = 3 輸出: [1,3,3,1]
示例 2:
輸入: rowIndex = 0 輸出: [1]
示例 3:
輸入: rowIndex = 1 輸出: [1,1]
public List<Integer> getRow(int rowIndex) {List<List<Integer>> list = new ArrayList<List<Integer>>();for(int i = 0; i <= rowIndex;i++){List<Integer> row = new ArrayList<Integer>();if(i==0){row.add(1);}else if(i==1){row.add(1);row.add(1);}else{for(int j=0;j<=i;j++){if(j==0 || j==i){row.add(1);}else {row.add(list.get(i-1).get(j-1)+list.get(i-1).get(j));}}}list.add(row);}return list.get(rowIndex);}