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

Pascal's Triangle II

  Total Accepted: 39663 Total Submissions: 134813My Submissions

 

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()

{



}

  

 

你可能感兴趣的:(LeetCode)