目录
            
            
            
题目
Given numRows, generate the first numRows of Pascal’s triangle.
Example:
given numRows = 5,
Return
[
     [1],
    [1,1],
   [1,2,1],
  [1,3,3,1],
 [1,4,6,4,1]
]
解决方案
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 
 | public class Solution {public List<List<Integer>> generate(int numRows) {
 if(numRows == 0) return new ArrayList<List<Integer>>();
 
 List<List<Integer>> result = new ArrayList<List<Integer>>();
 
 for(int i = 1; i <= numRows; i++){
 List<Integer> curr = new ArrayList<Integer>();
 curr.add(1);
 for(int j = 1; j < (i-1); j++){
 curr.add(result.get(i-2).get(j-1) + result.get(i-2).get(j));
 }
 if(i > 1){
 curr.add(1);
 }
 result.add(curr);
 }
 return result;
 }
 }
 
 | 
注意事项
- 构建杨辉三角,当输入层数为0时特殊处理
- 每一层至少有第一个元素1,从第二层开始,每层至少有最后一个元素1
- 从第三层开始,可以使用上一层“头顶”的两个元素求和。