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

119. Pascal's Triangle II

时间:2016-04-12 23:53:06      阅读:225      评论: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         
 5         vector<int> triangle(rowIndex + 1);
 6         triangle[0] = 1;
 7 
 8         for (int i = 0; i < rowIndex; ++i)
 9         {
10             for (int j = i + 1; j > 0; --j)
11             {
12                 triangle[j] += triangle[j - 1];
13             }
14         }
15 
16         return triangle;
17     }
18 };

 

119. Pascal's Triangle II

标签:

原文地址:http://www.cnblogs.com/lijiatu/p/5384934.html

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