首页 > 其他 > 详细

[Leetcode] Pascal's Triangle

时间:2014-11-02 07:03:58      阅读:300      评论:0      收藏:0      [点我收藏+]

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

原文:http://www.cnblogs.com/Phoebe815/p/4068324.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!