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

# 118.Pascals Triangle

**118.Pascals Traingle**

难度:Easy

> 给定一个非负整数 numRows，生成杨辉三角的前 numRows 行。 ![](https://upload.wikimedia.org/wikipedia/commons/0/0d/PascalTriangleAnimated2.gif) 在杨辉三角中，每个数是它左上方和右上方的数的和。

示例:

```
输入: 5
输出:
[
     [1],
    [1,1],
   [1,2,1],
  [1,3,3,1],
 [1,4,6,4,1]
]
```

代码如下：

```
class Solution {
public:
    vector<vector<int>> generate(int numRows) {
        
        if(numRows<=tri.size()) 
        {
            vector<vector<int>> res;
            for(int i=0;i<numRows;i++)
                res.push_back(tri[i]);
            return res;
        }
        int n=tri.size();
        while(n<numRows)
        {
            vector<int> tmp;
            tmp.push_back(tri[n-1][0]);
            for(int i=1;i<n;i++)
                tmp.push_back(tri[n-1][i]+tri[n-1][i-1]);
            tmp.push_back(tri[n-1][n-1]);
            tri.push_back(tmp);
            n++;
        }
        return tri;
    }
private:
    vector<vector<int>>tri={ {1},{1,1} };
};
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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, and the optional `goal` query parameter:

```
GET https://dfine.gitbook.io/leetcode/118.pascals_triangle.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
