问题:
方法一:
class Solution {
public List<List<Integer>> generate(int numRows) {
List<List<Integer>> results = new ArrayList<>();
for(int i = 1; i <= numRows; i++){
List<Integer> tmp = new ArrayList<>();
for(int j = 1; j <= i; j++){
if(i <= 2){
tmp.add(1);
}else{
if(j > 1 && j <i){
int sum = results.get(i-2).get(j-2) + results.get(i-2).get(j-1);
tmp.add(sum);
}else{
tmp.add(1);
}
}
}
results.add(tmp);
}
return results;
}
}
方法二:方法二和方法一本质没有区别
class Solution {
public List<List<Integer>> generate(int numRows) {
List<List<Integer>> ret = new ArrayList<List<Integer>>();
for (int i = 0; i < numRows; ++i) {
List<Integer> row = new ArrayList<Integer>();
for (int j = 0; j <= i; ++j) {
if (j == 0 || j == i) {
row.add(1);
} else {
row.add(ret.get(i - 1).get(j - 1) + ret.get(i - 1).get(j));
}
}
ret.add(row);
}
return ret;
}
}
链接:https://leetcode-cn.com/problems/pascals-triangle/solution/yang-hui-san-jiao-by-leetcode-solution-lew9/
来源:力扣(LeetCode)