DescriptionHintsSubmissionsDiscussSolution Given a non-negative integer numRows, generate the first numRows of Pascal's triangle. In Pascal's triangle, each number is the sum of the two numbers directly above it. Example: Input: 5 Output: [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] 一會看下被人的code Dp https://leetcode.com/problems/pascals-triangle/solution/ class Solution { public List<List<Integer>> generate(int numRows) { int n = numRows; int[][] res = new int[n][n]; // fill in the base case // fill in the first and the last on each row with 1 for(int i = 0; i < n; i++){ // row is I, col is 0, and I res[i][0] = 1; res[i][i] = 1; } // induction rule // current = top + top left // top has the same col, row - 1 // top left is row - 1, col - 1 // start from the third row, always start from the second col, ending at the last col - 1 // last col is I for(int r = 2; r < n; r++){ for(int c = 1; c < n - 1; c++){ res[r][c] = res[r - 1][c - 1] + res[r - 1][c]; } } List<List<Integer>> result = new ArrayList<>(); for(int i = 0; i < res.length; i++){ List<Integer> tmp = new ArrayList<>(); for(int j = 0; j < res[i].length; j++){ if(res[i][j] != 0){ tmp.add(res[i][j]); } } result.add(new ArrayList<>(tmp)); } return result; } } // others concise code , without converting int[][] to list<list<integer>> https://leetcode.com/problems/pascals-triangle/discuss/38141/My-concise-solution-in-Java