首页 > 其他 > 详细

[LeetCode]Pascal's Triangle

时间:2015-09-16 21:53:35      阅读:203      评论:0      收藏:0      [点我收藏+]

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

 

下一行根据上一行按照规律相加就行了,old变量记录上一行。

 1 class Solution {
 2 public:
 3     vector<vector<int>> generate(int numRows) {
 4         vector<vector<int>> result;
 5         if(numRows<1) return result;
 6         vector<int> old;
 7         for(int i=0;i<numRows;i++)
 8         {
 9             vector<int> f(i+1,1);
10             for(int j=1;j<i;j++)
11             {
12                 f[j]=old[j-1]+old[j];
13             }
14             result.push_back(f);
15             old = f;
16         }
17         return result;
18     }
19 };

 

[LeetCode]Pascal's Triangle

原文:http://www.cnblogs.com/Sean-le/p/4814399.html

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