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

解题思路:

参考百度百科:杨辉三角

 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 };

 




posted @ 2015-11-03 15:52  skycore  阅读(121)  评论(0编辑  收藏  举报