手机网站 需求模板,整站优化服务,wordpress魔改,苏州关键词排名提升给定一个非负整数 numRows#xff0c;生成「杨辉三角」的前 numRows 行。 在「杨辉三角」中#xff0c;每个数是它左上方和右上方的数的和。 class Solution {public ListListInteger generate(int numRows) {ListListInteger res new Arra…给定一个非负整数 numRows生成「杨辉三角」的前 numRows 行。 在「杨辉三角」中每个数是它左上方和右上方的数的和。 class Solution {public ListListInteger generate(int numRows) {ListListInteger res new ArrayList();for (int i 0; i numRows; i) {ListInteger list new ArrayList();for (int j 0; j i; j) {if (j 0 || j i) {list.add(1);} else {list.add(res.get(i - 1).get(j - 1) res.get(i - 1).get(j));}}res.add(list);}return res;}
}复杂度分析 时间复杂度O(n²n是numRows。
空间复杂度O(1)。不考虑返回值的空间占用。