您的位置:首页 > 其它

leetcode_119——Pascal's Triangle II (简单题,简单的递归)

2015-05-04 18:10 429 查看

Pascal's Triangle II

Total Accepted: 39663 Total Submissions: 134813My Submissions
Question Solution

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?

Hide Tags

Array

Have you met this question in a real intervie

这道题是个简单题,用递归就可以了

#include<iostream>
#include<vector>
using namespace std;

vector<int> getRow(int rowIndex) {
vector<int> vec;
vector<int> temp;
if(rowIndex==0)
{
vec.push_back(1);
return vec;
}
if(rowIndex==1)
{
vec.push_back(1),vec.push_back(1);
return vec;
}
temp=getRow(rowIndex-1);
vec.push_back(1);
int len=temp.size();
for(int i=0;i<len-1;i++)
vec.push_back(temp[i]+temp[i+1]);
vec.push_back(1);
return vec;
}
int main()
{

}


  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: