【leetcode】Pascal’s Triangle II
1,883 views
0
Question:
Given an index k , return the k th 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 ?
Anwser 1:
class Solution { public: vector<int> getRow(int rowIndex) { // Start typing your C/C++ solution below // DO NOT write int main() function vector<int> ret; for(int i = 0; i <= rowIndex; i++){ if(i == 0) { ret.push_back(1); continue; } for(int j = i; j >= 0; j--){ // from end to begin if(j == 0) { ret[0] = 1; } else if(j == i){ ret.push_back(1); }else { ret[j] = ret[j-1] + ret[j]; } } } return ret; } };
版权所有: 本文系米扑博客原创、转载、摘录,或修订后发表,最后更新于 2015-11-27 19:12:57
侵权处理: 本个人博客,不盈利,若侵犯了您的作品权,请联系博主删除,莫恶意,索钱财,感谢!