> For the complete documentation index, see [llms.txt](https://timmybeeflin.gitbook.io/cracking-leetcode/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://timmybeeflin.gitbook.io/cracking-leetcode/dynamic-programming/118.-pascals-triangle.md).

# 118. Pascal's Triangle

![](/files/-MehGruaqusl3nMOTMvu)

![](/files/-MehGufJ-bgZ4o59dbJs)

time: O(n^2)

space: O(n)

```java
class Solution {
    public List<List<Integer>> generate(int numRows) {
        List<List<Integer>> res = new ArrayList<>();
        List<Integer> list = new ArrayList<>();
        for (int i = 0 ; i < numRows; i++) {
            list.add(0, 1);
            for (int j = 1; j < list.size() - 1; j++) {
                list.set(j, list.get(j) + list.get(j+1));
            }
            res.add(new ArrayList<>(list));
        }
        return res;
    }
}
```
