一个 leetcode 问题:给定 numRows,生成 Pascal 三角形的前 numRows。
该算法的 C++ 版本被 Leetcode 接受。谁能告诉我为什么这个 Java 版本不能被接受?
public class Solution {
public List<List<Integer>> generate(int numRows) {
List<List<Integer>> result = new ArrayList<List<Integer>>();
if (numRows == 0) return result;
List<Integer> raw = new ArrayList<Integer>();
raw.add(1);
result.add(raw);
if (numRows == 1) return result;
List<Integer> row = raw;
List<Integer> row2 = raw;
for (int i = 2; i <= numRows; i++) {
for (int j = 0; j < row.size()-1; j++)
row2.add(row.get(j) + row.get(j+1));
row2.add(1);
result.add(row2);
row = row2;
row2 = raw;
}
return result;
}
}