网站规划建设与管理维护课后答案,常见的简单的app开发,wordpress恢复页面,河北秦皇岛建设局网站问题描述
给定一个非负整数 numRows#xff0c;生成杨辉三角的前 numRows 行。 在杨辉三角中#xff0c;每个数是它左上方和右上方的数的和。
示例:
输入: 5
输出:
[[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]
]
实现方法
class Solution {public ListListIntege…问题描述
给定一个非负整数 numRows生成杨辉三角的前 numRows 行。 在杨辉三角中每个数是它左上方和右上方的数的和。
示例:
输入: 5
输出:
[[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]
]
实现方法
class Solution {public ListListInteger generate(int numRows) {ListListInteger result new ArrayList();if (numRows 1) return result;for (int i 0; i numRows; i) {//扩容ListInteger line Arrays.asList(new Integer[i1]);line.set(0, 1); line.set(i, 1);for (int j 1; j i; j) {//等于上一行的左右两个数字之和line.set(j, result.get(i-1).get(j-1) result.get(i-1).get(j));}result.add(line);}return result; }
}
代码执行结果