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].


问题分析:

本题与Pascal's Triangle问题类似,可参考我之前的Pascal's Triangle问题及解法的博客。


具体过程详见代码:

class Solution {
public:
    vector getRow(int rowIndex) {
        vector last(1,1),res(1,1);
        if(rowIndex == 0) return res;
        for(int i = 2; i <= rowIndex + 1; i++)
        {
        	res.resize(i);
        	res[0] = 1;
        	for(int j = 0; j < i - 2; j++)
        	{
        		res[j + 1] = last[j] + last[j + 1]; 
			}
        	res[i - 1] = 1;
        	last = res;
		}
		return res;
    }
};



你可能感兴趣的:(Pascal's Triangle II问题及解法)