# 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: 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://dfine.gitbook.io/leetcode/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.
