首页 > 其他 > 详细

[LeetCode]Pascal's Triangle II

时间:2015-09-16 21:33:45      阅读:149      评论:0      收藏:0      [点我收藏+]

Pascal‘s Triangle II

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?

 

O(k)的空间,那么就直接初始化,然后按照Pascal‘s Triangle的规律,计算。

由于覆盖的问题,从后往前计算就可以避免。

 1 class Solution {
 2 public:
 3     vector<int> getRow(int rowIndex) {
 4         vector<int> result(rowIndex+1,1);
 5         for(int i=0;i<=rowIndex;i++)
 6         {
 7             for(int j=i-1;j>0;j--)
 8             {
 9                 result[j] = result[j-1]+result[j];
10             }
11         }
12         return result;
13     }
14 };

 

[LeetCode]Pascal's Triangle II

原文:http://www.cnblogs.com/Sean-le/p/4814405.html

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