[leetcode]Pascal's Triangle II
2014-07-22 01:01
459 查看
Pascal's Triangle II
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?
算法:
思路1:
跟[leetcode]Pascal's Triangle 几乎一样,用2k个空间就可以搞定
思路2:
对空间进行优化,用k个空间搞定
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?
算法:
思路1:
跟[leetcode]Pascal's Triangle 几乎一样,用2k个空间就可以搞定
思路2:
对空间进行优化,用k个空间搞定
public List<Integer> getRow(int rowIndex) { List<Integer> result = new ArrayList<Integer>(); if(rowIndex < 0) return result; result.add(1); for(int i = 1 ; i <= rowIndex;i++){ result = getNextRow(result); } return result; } private List<Integer> getNextRow(List<Integer> list){ int tem = 1; for(int i = 1; i < list.size(); i++){ int a = list.get(i); list.set(i, list.get(i) + tem); tem = a; } list.add(1); return list; }
相关文章推荐
- LeetCode(33)-Pascal's Triangle II
- 【leetcode】Pascal's Triangle II
- [LeetCode] Pascal's Triangle II 杨辉三角之二
- Pascal's Triangle II leetcode
- 【leetcode刷题笔记】Pascal's Triangle II
- [LeetCode 119] - 杨辉三角形II(Pascal's Triangle II)
- (leetcode)Pascal's Triangle II
- Leetcode: Pascal's Triangle II
- LeetCode——Pascal's Triangle II
- LeetCode:Pascal's Triangle I II
- [LeetCode] Pascal's Triangle II
- LeetCode(119) Pascal's Triangle II
- leetcode_119——Pascal's Triangle II (简单题,简单的递归)
- [leetcode]Pascal's Triangle II @ Python
- [LeetCode]Pascal's Triangle II
- [LeetCode] 119. Pascal's Triangle II 杨辉三角 II
- 学会从后往前遍历,例 [LeetCode] Pascal's Triangle II,剑指Offer 题4
- 【LeetCode】Pascal's Triangle II (杨辉三角)
- LeetCode-Pascal's Triangle II-帕斯卡三角-DP
- 【leetcode】Pascal's Triangle I & II (middle)