本文主要是介绍LeetCode(33)-Pascal's Triangle II,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
题目:
Given an index k, return the kth row of the Pascal's triangle.For example, given k = 3,
Return [1,3,3,1].
思路:
- 要求是返回帕斯卡某一行的数据
- 设置两个list变量,pre和next,pre用来存储当前的最后的最后一行,next存储下一行,next赋值给pre(在这之前pre清空,之后next清空),往下移动
- -
代码:
public class Solution {public List<Integer> getRow(int rowIndex) {List<Integer> pre = new ArrayList<Integer>();List<Integer> next = new ArrayList<Integer>();pre.add(1);if(rowIndex == 0){return pre;}for(int i = 1;i < rowIndex+1;i++){next.add(1);for(int a = 1;a < i;a++){next.add(pre.get(a-1)+pre.get(a));}next.add(1);pre.clear();for(int o =0;o < next.size();o++ ){pre.add(next.get(o));}next.clear();}return pre;}
}
这篇关于LeetCode(33)-Pascal's Triangle II的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!