首页 > 代码库 > [Leetcode] Pascal's Triangle

[Leetcode] 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]]

Solution:

 1 public class Solution { 2     public List<List<Integer>> generate(int numRows) { 3         List<List<Integer>> ret=new ArrayList<List<Integer>>(); 4         if(numRows<1) 5             return ret; 6         List<Integer> al=new ArrayList<Integer>(); 7         al.add(1); 8         ret.add(al); 9         for(int i=1;i<numRows;++i){10             List<Integer> cur=new ArrayList<Integer>();11             cur.add(1);12             for(int j=1;j<al.size();++j){13                 cur.add(al.get(j-1)+al.get(j));14             }15             cur.add(1);16             ret.add(cur);17             al=cur;18         }19         return ret;20     }21 }

 

[Leetcode] Pascal's Triangle