现在的位置: 首页 > 综合 > 正文

Pascal’s Triangle

2017年12月23日 ⁄ 综合 ⁄ 共 516字 ⁄ 字号 评论关闭

Given numRows, generate the first numRows of
Pascal's triangle.

For example, given numRows = 5,

Return

[
     [1],
    [1,1],
   [1,2,1],
  [1,3,3,1],
 [1,4,6,4,1]
]

比较直观,注意我偷懒定义了两个宏

#define vi vector<int>
#define vvi vector<vi >
class Solution {
public:
    vvi generate(int row)
{
    if(row<=0)
		return vvi();
	vvi ans(1,vi(1,1));
	int preLine=0;
	vi curLine;
	while(preLine<row-1)
	{
		curLine.clear();
		curLine.push_back(1);
		int k=ans[preLine].size();
		for(int i=1;i<k;i++)
			curLine.push_back(ans[preLine][i-1]+ans[preLine][i]);
		curLine.push_back(1);
		ans.push_back(curLine);
		preLine++;
	}
	return ans;
}
		

};

抱歉!评论已关闭.