我在教科书中看到以下代码
public static int[][] Pascal(int N) {
int[][] result = new int[N][]; // Build a grid with specific number of rolls
for (int i = 0; i < N; i += 1) {
result[i] = new int[i + 1]; // Build an empty row with length
result[i][0] = result[i][i] = 1;
for (int j = 1; j < i; j += 1) {
result[i][j] = result[i - 1][j - 1] + result[i - 1][j];
}
}
return result;
}
对于这个帕斯卡三角形如何实现,我毫不怀疑。
但是,我之前曾学过创建数组,必须指定长度,
例如int[] A = int[];
会给我Error
必须为int[] A = int[N];
或int[] A = int[]{1,2,3,etc.};
因此,当我们创建数组网格int[][] result = new int[N][];
时
如何让我仅指定行数,而将每行的长度保留为空白?
我确实注意到每个卷的长度是在稍后的result[i] = new int[i + 1];
上定义的,但我仍然不明白为什么允许启动此网格。
谢谢!