首页 > 其他 > 详细

[LeetCode]Pascal's Triangle

时间:2015-11-03 16:01:39      阅读:194      评论: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>> result;
 5         int s = 1;
 6         for (int i = 1; i <= numRows; ++i) {
 7             vector<int> line;
 8             line.push_back(1);
 9             if (i == 1) {
10                 result.push_back(line);
11                 continue;
12             }
13             
14             for (int j = 1; j <= i - 2; ++j) {
15                 line.push_back(s = ((i - j) * s) / j);
16             }
17             
18             line.push_back(1);
19             result.push_back(line);
20             s = 1;
21         }
22         
23         return result;
24     }
25 };

 




[LeetCode]Pascal's Triangle

原文:http://www.cnblogs.com/skycore/p/4933438.html

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