
行数:i ; 行内下标:j
每行元素个数:i+1 ; 元素值:[i-1][j-1] + [i-1][j]
*/
class Solution {
/**
行数:i ; 行内下标:j
每行元素个数:i+1 ; 元素值:[i-1][j-1] + [i-1][j]
*/
public List<List<Integer>> generate(int numRows) {
List<List<Integer>> result = new ArrayList<>();
for(int i = 0; i < numRows; i++) {
//每行元素个数i+1
List<Integer> temp = new ArrayList<>(i + 1);
//首尾初始化为1
temp.add(0,1);
if(i != 0) { //避免i=0时,初始化过多的元素
temp.add(temp.size() - 1,1);
}
//j-->[1,i) 因为首尾已经提前初始化了
for(int j = 1; j < i; j++) {
//元素值:[i-1][j-1] + [i-1][j]
int tempNumber = result.get(i - 1).get(j-1) + result.get(i - 1).get(j);
temp.add(j,tempNumber);
}
result.add(temp);
}
return result;
}
}
487

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



