LeetCode——118. 杨辉三角
通过万岁!!!
- 题目:构建一个杨辉三角,三角形左边和右边都是1,nums[i][j] = num[i-1][j-1] + nums[i-1][j]。
- 思路:就按照规则实现就好了
- 技巧:遍历
java代码
class Solution {public List<List<Integer>> generate(int numRows) {List<List<Integer>> res = new ArrayList<>();for (int i = 0; i < numRows; i++) {List<Integer> tmp = new ArrayList<>();for (int j = 0; j <= i; j++) {// 第一列和最后一列为1if (j == 0 || i == j) {tmp.add(1);} else {tmp.add(res.get(i - 1).get(j - 1) + res.get(i - 1).get(j));}}res.add(tmp);}return res;}
}
- 总结:这个题目比较简单,也没啥好说的了。