标签:
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?
这道题是要在O(k)个空间内,找到第k行数据。如果直接顺序计算a[i]=a[i]+[i-1],由于a[i]被替换,所以,在计算a[i+1]时候就出错了。所以使用倒序计算
class Solution { public: vector<int> getRow(int rowIndex) { vector<int> a; a.resize(rowIndex+1,0); a[0]=1; for(int k =0;k <rowIndex +1;k++) { for(int i =k;i>0;i--) { a[i]=a[i-1]+a[i]; } } return a; } };
(leetcode)Pascal's Triangle II
标签:
原文地址:http://www.cnblogs.com/chdxiaoming/p/4478438.html