1。采用动态规划 即n的值 是有n-1的值推导出来的。推导过程就是
i的值等于上面i的值加上i+1的值。注意i的取值范围。
class Solution {
public List<List<Integer>> generate(int numRows) {
List<List<Integer>> result = new ArrayList<>();
for (int i = 1; i <= numRows; i++) {
result.add(yanghui(i));
}
return result;
}
public List<Integer> yanghui(int n) {
List<Integer> result = new ArrayList<>();
if (n == 1) {
result.add(1);
} else if (n == 2) {
result.add(1);
result.add(1);
} else {
List<Integer> backList = yanghui(n - 1);
int length = backList.size(); //1 1
List<Integer> tempList = new ArrayList<>(length + 1);
tempList.add(1);
for (int i = 0; i < length-1; i++) {
tempList.add(backList.get(i) + backList.get(i + 1));
}
tempList.add(1);
result.addAll(tempList);
}
return result;
}
}