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

# 118. Pascal's Triangle

## 1.問題&#x20;

* 給予一個numRows, 產生一個帕斯卡三角形

![](/files/-LNL5CUKqzHd6eZR2UMF)

## 2.想法 <a href="#id-2-xiang-fa" id="id-2-xiang-fa"></a>

* 提問
* function header, parameter
* test input
* 說明想法
* 測試計算複雜度

## **3.程式碼** <a href="#id-3-cheng-shi" id="id-3-cheng-shi"></a>

```
class Solution {
public:
    vector<vector<int>> generate(int numRows) {
        if (numRows == 0) {
            return {};
        }
        
        vector<vector<int>> res;
        res.push_back({1});
        
        for (int i = 0; i < numRows - 1; i++) {
            int size = res[i].size();
            vector<int> record;
            record.push_back(res[i][0]);
            for (int j = 1; j < size; j++) {
                record.push_back(res[i][j - 1] + res[i][j]);
            }
            record.push_back(res[i][size - 1]);
            res.push_back(record);
        }
        
        return res;
    }
};
```
