1. 程式人生 > >Pascal's Triangle II 生成楊輝三角中的某行

Pascal's Triangle II 生成楊輝三角中的某行

題目:

連結

解答:

在上一題基礎上修改,注意行號。

程式碼:

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