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

LeetCode - Refresh - Pascal's Triangle

时间:2015-03-21 18:28:23      阅读:98      评论:0      收藏:0      [点我收藏+]

标签:

This is simple. Just everything use current[j] += current[j-1]. But leave the first one to be "1". Then add another "1" to end.

 

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

 

LeetCode - Refresh - Pascal's Triangle

标签:

原文地址:http://www.cnblogs.com/shuashuashua/p/4355879.html

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