力扣
https://leetcode-cn.com/problems/pascals-triangle/
难度简单617
给定一个非负整数 numRows,生成「杨辉三角」的前 numRows 行。
在「杨辉三角」中,每个数是它左上方和右上方的数的和。

示例 1:
输入: numRows = 5 输出: [[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]]
示例 2:
输入: numRows = 1 输出: [[1]]
提示:
1 <= numRows <= 30
class Solution {
public List<List<Integer>> generate(int numRows) {
//ans[i][j]=ans[i-1][j]+ans[i-1][j-1] 算法
List<List<Integer>> ans = new ArrayList<List<Integer>>();
for(int i=0;i<numRows;i++)
{
List<Integer> row = new ArrayList<Integer>();
for(int j=0;j<=i;j++)
{
if(j==0 || j==i) row.add(1);
else
{
row.add(ans.get(i-1).get(j-1)+ans.get(i-1).get(j));
}
}
ans.add(row);
}
return ans;
}
}

905

被折叠的 条评论
为什么被折叠?



