原题网址:https://leetcode.com/problems/pascals-triangle/
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>> triangles = new ArrayList<>();
for(int i=0; i<numRows; i++) {
List<Integer> triangle = new ArrayList<>();
triangles.add(triangle);
for(int j=0; j<=i; j++) {
if (j==0 || j==i) triangle.add(1);
else triangle.add(triangles.get(i-1).get(j-1) + triangles.get(i-1).get(j));
}
}
return triangles;
}
}

本文介绍了一种生成帕斯卡三角形的方法,通过Java实现,详细展示了如何根据输入的行数来生成相应大小的帕斯卡三角形,并提供了完整的代码示例。
676

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



