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

Pascal's Triangle II

时间:2015-04-13 14:32:59      阅读:108      评论:0      收藏:0      [点我收藏+]

标签:

Given an index k, return the kth row of the Pascal‘s triangle.

For example, given k = 3,
Return [1,3,3,1].

Note:
Could you optimize your algorithm to use only O(k) extra space?

 

 1 class Solution {
 2 public:
 3     vector<int> getRow(int rowIndex) {
 4         vector<int> row;
 5         row.push_back(1);
 6         if(rowIndex == 0) return row;
 7         
 8         row.push_back(1);
 9         if(rowIndex == 1) return row;
10         
11         for(int i = 2; i <= rowIndex; i++){
12             vector<int> temp;
13             temp.push_back(1);
14             for(int j = 1; j < i; j++)
15                 temp.push_back(row[j-1] + row[j]);
16             temp.push_back(1);
17             row = temp;
18         }
19         return row;
20     }
21 };

 

Pascal's Triangle II

标签:

原文地址:http://www.cnblogs.com/amazingzoe/p/4422000.html

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