码迷,mamicode.com
首页 > 其他好文 > 详细

[Leetcode] Pascal's Triangle

时间:2014-11-02 07:04:34      阅读:190      评论:0      收藏:0      [点我收藏+]

标签:style   blog   io   color   ar   for   sp   div   on   

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

标签:style   blog   io   color   ar   for   sp   div   on   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!