Given numRows, generate the first numRows of Pascal's triangle.
For example, given numRows = 5,
Return
Return
[ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ]Solution 1: simple looping
public class Solution {
public List<List<Integer>> generate(int numRows) {
List<List<Integer>> res=new ArrayList<>();
for (int i=0; i<numRows; i++) {
List<Integer> one=new ArrayList<>();
for (int j=0; j<=i; j++) {
if (j==0 || j==i) one.add(1);
else one.add(res.get(i-1).get(j-1)+res.get(i-1).get(j));
}
res.add(one);
}
return res;
}
}
没有评论:
发表评论