Given numRows, generate the first numRows of Pascal's triangle.
For example, given numRows = 5,
Return
[ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ]迭代 非遞歸版本
public class Solution { public List> generate(int numRows) { List
> res = new ArrayList<>(); if(numRows<=0) return res; List
lin = new ArrayList<>(); lin.add(1); res.add(lin); if(numRows==1) return res; for(int i=0;i list = new ArrayList<>(); List pre = res.get(i); list.add(1); for(int j=0;j
遞歸版本public class Solution { List> res = new ArrayList<>(); public List
> generate(int numRows) { if(numRows<=0) return res; helpGenerate(numRows); return res; } private List
helpGenerate(int numRows){ if(numRows == 1){ List list = new ArrayList<>(); list.add(1); res.add(list); return list; } List pre = helpGenerate(numRows-1); List list = new ArrayList<>(); list.add(1); for(int i=0;i