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

Note:
Could you optimize your algorithm to use only O(k) extra space?

代码:

class Solution{
public:
	vector<int> getRow(int rowIndex) {
		vector<int> vec1(rowIndex + 1);
		vector<int> vec2(rowIndex + 1);
		vec1[0] = 1;
		vec2[0] = 1;
		for (int i = 1; i <= rowIndex; ++i)
		{
			vec2[0] = 1;
			for (int j = 1; j < i; ++j)
			{
				vec2[j] = vec1[j] + vec1[j - 1];
			}
			vec2[i] = 1;
			vec1 = vec2;
		}
		return vec2;
	}
};


你可能感兴趣的:(Algorithm,LeetCode,C++,c,pascal)