1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| class Solution { public List<List<Integer>> generate(int numRows) { List<List<Integer>> ans = new LinkedList<>(); if (numRows == 0) { return ans; } ans.add(List.of(1)); if (numRows == 1) { return ans; } ans.add(List.of(1, 1)); for (int i = 2; i < numRows; i++) { List<Integer> list = new LinkedList<>(); list.add(1); for (int j = 0; j + 1 < ans.get(i - 1).size(); j++) { list.add(ans.get(i - 1).get(j) + ans.get(i - 1).get(j + 1)); } list.add(1); ans.add(list); } return ans; } }
|