Pascal's Triangle
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] ]
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 public class Solution { 2 public ArrayList<ArrayList<Integer>> generate(int numRows) { 3 ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>(); 4 if(numRows==0) return res; 5 ArrayList<Integer> one = new ArrayList<Integer>(); 6 one.add(1); 7 res.add(one); 8 if(numRows==1) return res; 9 ArrayList<Integer> two = new ArrayList<Integer>(); 10 two.add(1); 11 two.add(1); 12 res.add(two); 13 if(numRows==2) return res; 14 for(int i=3;i<=numRows;i++){ 15 ArrayList<Integer> last = res.get(res.size()-1); 16 ArrayList<Integer> cur = new ArrayList<Integer>(); 17 for(int j=0;j<i;j++){ 18 if(j==0 || j==i-1){ 19 cur.add(1); 20 } 21 else{ 22 cur.add(last.get(j)+last.get(j-1)); 23 } 24 } 25 res.add(cur); 26 } 27 return res; 28 } 29 }