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 getRow(int rowIndex) {
List pre = new ArrayList();
List next = new ArrayList();
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;
}
}