给定一个非负整数 numRows
,生成「杨辉三角」的前 numRows
行。
示例 1:
输入: numRows = 5 输出: [[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]]
示例 2:
输入: numRows = 1 输出: [[1]]
思路:用动态规划
class Solution {
public List<List<Integer>> generate(int numRows) {
List<List<Integer>> res=new ArrayList<>();
for(int i=0;i<numRows;i++){
List<Integer> ans=new ArrayList<>();
for(int j=0;j<=i;j++){
if(j==0||j==i){
ans.add(1);
}else{
ans.add(res.get(i-1).get(j-1)+res.get(i-1).get(j));
}
}
res.add(ans);
}
return res;
}
}