[LeetCode][119. Pascal's Triangle II][easy]Java实现,杨辉三角
2017-09-26 15:48
537 查看
原题:
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?
思路:此题是计算杨辉三角的题,比较简单,只要知道杨辉三角是怎么计算来的就可以写出
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?
思路:此题是计算杨辉三角的题,比较简单,只要知道杨辉三角是怎么计算来的就可以写出
public List<Integer> getRow(int rowIndex) { List<Integer> list = new ArrayList<>(); if (rowIndex < 0) return list; list.add(1); if (rowIndex == 0) return list; for (int i = 1; i <= rowIndex ; i++) { for (int j = list.size()-1; j > 0; j--) { list.set(j,list.get(j-1)+list.get(j)); } list.add(1); } return list; }
相关文章推荐
- 【LeetCode-面试算法经典-Java实现】【119-Pascal's Triangle II(帕斯卡三角形(杨辉三角)II)】
- [Leetcode 118 and 119, Easy] Pascal's Triangle (I and II)
- LeetCode-Easy部分中标签为Array#119 : Pascal’s Triangle II
- [leetCode 118 & 119] Pascal's Triangle I && II (杨辉三角问题)
- ※ Leetcode - Dynamic Programming - 119. Pascal's Triangle II(倒叙滚动数组求解杨辉三角第k行)
- Leetcode 119. Pascal's Triangle II 杨辉三角2 解题报告
- 【LeetCode】119.Pascal's Triangle II(Easy)解题报告
- 【LeetCode——JAVA实现】119. pascals-triangle-ii(帕斯卡三角形 II)
- LeetCode-119:Pascal's Triangle II (返回杨辉三角指定行)
- LeetCode119. Pascal's Triangle II-python(easy)
- Leetcode:119. Pascal's Triangle II(JAVA)
- 【leetcode】119. Pascal's Triangle II【java】
- Leetcode#119. Pascal's Triangle II(杨辉三角II)
- 基于Java实现杨辉三角 LeetCode Pascal's Triangle
- Leetcode 细节实现 Pascal's Triangle II
- LeetCode 119:Pascal's Triangle II
- 【leetcode】Pascal's Triangle II(Java)
- [Leetcode]#119 Pascal's Triangle II
- 【leetcode】【119】Pascal's Triangle II
- leetcode[119]:Pascal's Triangle II