(leetcode)Pascal's Triangle II
2015-05-05 10:20
399 查看
Given an index k, return the kth row of the Pascal's triangle.
For example, given k = 3,
Return
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]时候就出错了。所以使用倒序计算
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
- Leetcode: Pascal's Triangle II
- [LeetCode]Pascal's Triangle II
- 学会从后往前遍历,例 [LeetCode] Pascal's Triangle II,剑指Offer 题4
- LeetCode(119) Pascal's Triangle II
- LeetCode:Pascal's Triangle I II
- [LeetCode] 119. Pascal's Triangle II 杨辉三角 II
- 每天一道LeetCode--119.Pascal's Triangle II(杨辉三角)
- LeetCode——Pascal's Triangle II
- LeetCode-Pascal's Triangle II-帕斯卡三角-DP
- 【LeetCode】Pascal's Triangle II (杨辉三角)
- Leetcode 119 Pascal's Triangle II 数论递推
- Pascal's Triangle II Leetcode java
- [LeetCode] Pascal's Triangle II
- leetcode_119——Pascal's Triangle II (简单题,简单的递归)
- LeetCode(33)-Pascal's Triangle II
- 【leetcode】Pascal's Triangle I & II (middle)
- LeetCode(33)-Pascal's Triangle II
- [Leetcode][JAVA] Pascal's Triangle I, II
- 【leetcode】Pascal's Triangle I&II