您的位置:首页 > 其它

LeetCode——Pascal's Triangle II

2015-06-19 21:53 441 查看
Description:

Given an index k, return the kth row of the Pascal's triangle.

For example, given k = 3, Return
[1,3,3,1]
.

public class Solution {
public List<Integer> getRow(int rowIndex) {
List<List<Integer>> list = new ArrayList<List<Integer>>();
for(int i=0; i<=rowIndex; i++) {
List<Integer> tList = new ArrayList<Integer>();
if(i == 0) {
tList.add(1);
}
else {
for(int j=0; j<=i; j++) {
if(j == 0 || j == i) {
tList.add(1);
}
else {
tList.add(list.get(i-1).get(j) + list.get(i-1).get(j-1));
}
}
}
list.add(tList);
}
return list.get(rowIndex);

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: