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

LeetCode OJ:Pascal's Triangle(帕斯卡三角)

时间:2015-10-28 22:44:22      阅读:268      评论: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]
]

帕斯卡三角,很简单的问题,见代码:

 1 class Solution {
 2 public:
 3     vector<vector<int>> generate(int numRows) {
 4         vector<vector<int>> ret;
 5         vector<int> tmpVec;
 6         ret.clear();
 7         tmpVec.clear();
 8         for(int i = 0; i < numRows; ++i){
 9             if(i == 0){
10                 tmpVec.push_back(1);
11             }else{
12                 for(int j = 0; j <= i; ++j){
13                     if(j == 0) tmpVec.push_back(1);
14                     else if(j == i) tmpVec.push_back(1);
15                     else tmpVec.push_back(ret[i - 1][j - 1] + ret[i - 1][j]);
16                 }
17             }
18             ret.push_back(tmpVec);
19             tmpVec.clear();
20         }
21         return ret;
22     }
23 };

 

LeetCode OJ:Pascal's Triangle(帕斯卡三角)

标签:

原文地址:http://www.cnblogs.com/-wang-cheng/p/4918826.html

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