杨辉三角
题目描述:
定一个非负整数 numRows,生成杨辉三角的前 numRows 行。在杨辉三角中,每个数是它左上方和右上方的数的和。
class Solution {
public List<List<Integer>> generate(int numRows) {
ArrayList<List<Integer>> result = new ArrayList<List<Integer>>();
for(int i = 0; i<numRows ; i++){
ArrayList<Integer> temp = new ArrayList<Integer>();
for (int j = 0; j <= i; j++) {
if (j == 0 || j == i) {
temp.add(1);
}else temp.add(result.get(i - 1).get(j - 1) + result.get(i - 1).get(j));
}
result.add(temp);
}
return result;
}
}
按照题意即可。详细请看代码,有疑问欢迎留言。