# Problem 118: Pascal's Triangle

> <https://leetcode.com/problems/pascals-triangle/>

## 思路

![](/files/-Lpv9yNp_Cn_PGoMH5_L)

* 首先这是杨辉三角形的一个生成
* 每次先把每行第一个 ‘1’ 打印出来，然后后面的元素就是上一行的两个元素的和
* 对于最后一个 ‘1’， 因为上一行比下一行少一个元素，所以对于本行的最后一个元素来说，就是上一行的最后一个元素加上一个 “空元素”，所以还是 ‘1’

```java
public class Solution {
    public List<List<Integer>> generate(int numRows) {
        List<Integer> rowRes = new ArrayList<Integer>();
        List<List<Integer>> res = new ArrayList(rowRes);  
        if (numRows == 0) {
            return res;
        }

        for (int i = 0; i < numRows; i++) {
            rowRes.add(0, 1);
            for (int j = 1; j < rowRes.size() - 1; j++) {
                rowRes.set(j, rowRes.get(j) + rowRes.get(j + 1));
            }
            res.add(new ArrayList(rowRes));
        }

        return res;
    }
}
```

## 易错点

1. ArrayList  的 add() 方法

   ```java
   rowRes.add(0, 1);
   ```

   这里的意思是，在 index 为 0 的地方，赋值为 1
2. ArrayList 加了元素以后，后面的元素自动顺延，而不是替代

   ![](/files/-Lpv9yNrw1llC57LfheV)
3. 上一个问题也恰巧解释了 set 方法中的 index 问题： 因为添加元素后，之前的元素顺延了，index 也发生了改变

   ```java
   rowRes.set(j, rowRes.get(j) + rowRes.get(j + 1));
   ```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://liuyang89116.gitbook.io/my-leetcode-book/post_chapter_3_array/problem_118_pascals_triangle.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
