LeetCode Pascal's Triangle II
来源:互联网 发布:淘宝卖家怎么提高流量 编辑:程序博客网 时间:2024/05/29 20:01
题目:
Given an index k, return the kth row of the Pascal's triangle.
For example, given k = 3,
Return [1,3,3,1]
.
就是给定一个K值,然后计算出第K行的杨辉三角。注意的是,这里的虽说是第K列,但是其实计数的时候是从0开始计数,这与前一篇博客中的那一题不同,那题是从第1行开始计数的。所以需要转化下,思路还是一样,一行一行来得到内容,只是最后输出的时候只需要输出第K列即可。相关的两题之间的联系可以参看前一篇博客。
public class Solution { public List<Integer> getRow(int rowIndex){List<List<Integer>> list = new ArrayList<List<Integer>>();List<Integer> li = new ArrayList<Integer>();if(rowIndex == 0){li.add(1);list.add(li);return li;}else{List<Integer> first = new ArrayList<Integer>();first.add(1);list.add(first);for(int i = 1; i <= rowIndex; i++){List<Integer> currentRow = new ArrayList<Integer>(); currentRow.add(1); for(int j = 1; j <= (i - 1); j++) { List<Integer> previousRow = list.get(i - 1); currentRow.add(previousRow.get(j - 1) + previousRow.get(j)); } currentRow.add(1); list.add(currentRow);}return list.get(rowIndex);}}}
0 0
- LeetCode Pascal's Triangle II
- LeetCode: Pascal's Triangle II
- [LeetCode] Pascal's Triangle II
- [Leetcode] Pascal's Triangle II
- 【leetcode】Pascal's Triangle II
- LeetCode - Pascal's Triangle II
- Leetcode: Pascal's Triangle II
- LeetCode-Pascal's Triangle II
- [leetcode] 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] Pascal's Triangle II
- Leetcode: Pascal's Triangle II
- Leetcode Pascal's Triangle II
- LeetCode Pascal's Triangle II
- LeetCode Pascal's Triangle II
- Discuz X3.2 论坛搬家教程
- OpengGL第八版的第一个例子
- KVM下DHCP无法取得IP的问题
- systemd
- SpringMVC 学习指南<一>
- LeetCode Pascal's Triangle II
- *Word Ladder II
- C#中实现多继承的方法
- 动力效果概念
- 同步的进一步了解
- 内存不能为read或written的解决方案
- 在C++中,为了让某个类只能通过new来创建实例(即如果直接创建对象,编译器将报错),怎样做?
- [Leetcode]Permutations
- LintCode-x的平方根