Pascal's Triangle
问题:
Given numRows, generate the first numRows of Pascal's triangle.
思路:
简单的数学推理
我的代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
public class Solution { public List<List<Integer>> generate(int numRows) { List<List<Integer>> rst = new ArrayList<List<Integer>>(); if(numRows < 1) return rst; List<Integer> first = new ArrayList<Integer>(); first.add(1); rst.add(first); for(int i = 2; i<= numRows; i++) { List<Integer> pre = rst.get(i - 2); List<Integer> now = new ArrayList<Integer>(); now.add(1); for(int j = 0; j < pre.size() - 1;j++) { now.add(pre.get(j) + pre.get(j + 1)); } now.add(1); rst.add(now); } return rst; } }