118. Pascal's Triangle

 

 1 class Solution {
 2     List<List<Integer>> res = new ArrayList<>();
 3     public List<List<Integer>> generate(int numRows) {
 4         helper(1, numRows, new ArrayList<>(), null);
 5         return res;
 6         
 7     }
 8     
 9     public void helper(int num, int total, List<Integer> list, List<Integer> prev) {
10         if(num > total) return;
11         
12         for(int i = 1; i <= num; i++) {
13             if(num == 1 || num == 2) {
14                 list.add(1);
15             }else {
16                 if(i == 1 || i == num) {
17                     list.add(1);
18                 }else {
19                     list.add(prev.get(i-2) + prev.get(i-1));//注意是i-2 循环用的是1开始
20                 } 
21             }
22         }
23         res.add(list);
24         helper(num+1, total, new ArrayList<>(), list);
25     }
26 }

 

posted @ 2018-09-22 00:25  jasoncool1  阅读(96)  评论(0编辑  收藏  举报