Given numRows, generate the first numRows of Pascal's triangle.
For example, given numRows = 5,
Return
[
[1],
[1,1],
[1,2,1],
[1,3,3,1],
[1,4,6,4,1]
]
public class Solution {
public List<List<Integer>> generate(int numRows) {
List<List<Integer>> res = new ArrayList<List<Integer>>();
if (numRows == 0) {
return res;
}
for (int i = 0; i < numRows; i++) {
ArrayList<Integer> arrayList = new ArrayList<Integer>();
arrayList.add(1);
for (int j = 1; j < i; j++) {
List<Integer> list = res.get(i-1);
int tmp = list.get(j-1) + list.get(j);
arrayList.add(tmp);
}
if (i != 0) {
arrayList.add(1);
}
res.add(arrayList);
}
return res;
}
}
本文详细介绍了如何使用Java代码生成指定行数的帕斯卡三角形,并提供了具体实现和示例。
1084

被折叠的 条评论
为什么被折叠?



