classSolution { public List<List<Integer>> generate(int numRows) {
// Create a numRows*numRows 2D array int[][] arr = newint[numRows][numRows]; for (inti=0; i < numRows; i++) { for (intj=0; j < i+1; j++) { /* If it is the first column or the last column, assign it as 1 Otherwise, assign it as the sum of "the number in the same column of the previous row" and "the number in the previous column of the previous row" */ if (j==0 || j==i) { arr[i][j] = 1; } else { arr[i][j] = arr[i-1][j] + arr[i-1][j-1]; } } }
// Put the 2D array into a nested list (discard the placeholder 0 in the 2D array) Listl=newArrayList(); for (inti=0; i < numRows; i++) { Listlist=newArrayList(); for (intj=0; j < i+1; j++) { list.add(arr[i][j]); } l.add(list); }