class Solution {
public List<List<Integer>> generate(int numRows) {
List<List<Integer>> result = new LinkedList<>();
if (numRows == 0) {
return result;
}
int[][] nums = new int[numRows][numRows];
for (int i = 0; i < numRows; i++) {
nums[i][0] = 1;
nums[i][i] = 1;
}
for (int i = 0; i < numRows; i++) {
for (int j = 0; j <= i; j++) {
if (nums[i][j] < 1) {
nums[i][j] = nums[i - 1][j - 1] + nums[i - 1][j];
}
}
}
for (int i = 0; i < numRows; i++){
List<Integer> list = new LinkedList<>();
for (int j = 0; j <= i;j++){
list.add(nums[i][j]);
}
result.add(list);
}
return result;
}
}
LeetCode118杨辉三角问题
最新推荐文章于 2024-11-07 20:44:05 发布