简单的编程题
public class Solution {
public ArrayList<ArrayList<Integer>> generate(int numRows) {
// Start typing your Java solution below
// DO NOT write main() function
ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();
if (numRows == 0)
return result;
ArrayList<Integer> first = new ArrayList<Integer>();
first.add(1);
result.add(first);
for (int i=2; i<=numRows; i++) {
ArrayList<Integer> row = new ArrayList<Integer>();
ArrayList<Integer> last = result.get(result.size()-1);
for (int j=0; j<last.size(); j++) {
int value = last.get(j);
if (j > 0)
value += last.get(j-1);
row.add(value);
}
row.add(1);
result.add(row);
}
return result;
}
}
本文介绍如何使用Java编程语言实现生成三角形数列的功能,包括定义类、方法及核心逻辑,通过循环和列表操作生成指定行数的三角形数列。
3万+

被折叠的 条评论
为什么被折叠?



