1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| class Solution { public List<List<Integer>> generate(int numRows) { List<List<Integer>> result = new ArrayList<>(); List<Integer> old = null; for(int i=0;i<numRows;i++){ List<Integer> list = new ArrayList<>();
for(int j =0;j<=i;j++){
int sum = 0; if(j==0||j==i) { sum = 1; } else{ sum = old.get(j)+(j>0?old.get(j-1):0);
} list.add(sum); } result.add(list); old = result.get(i); } return result; } }
|