首页 > 其他 > 详细

LeetCode 119:Pascal's Triangle II

时间:2017-05-06 18:18:17      阅读:338      评论: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].

题目要求计算杨辉三角某一行的元素。这个也是二项式系数的计算问题。

class Solution {
public:
    vector<int> getRow(int rowIndex) {
        vector<int> result;
		vector<int> tmp;
		result.push_back(1);
		if (rowIndex <= 0)
			return result;
		
		for (int i=1; i<=rowIndex; i++)
		{
			tmp=result;
			result.clear();
			result.push_back(1);
			for (int i=0; i<tmp.size()-1; i++)
			{
				result.push_back(tmp[i] + tmp[i+1]);
			}
			result.push_back(1);
				
		}
		return result;
    }
};


LeetCode 119:Pascal&#39;s Triangle II

原文:http://www.cnblogs.com/ljbguanli/p/6817475.html

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