# 119.Pascals Triangle II

**119.Pascals Triangle II**

难度:Easy

> 给定一个非负索引 k，其中 k ≤ 33，返回杨辉三角的第 k 行。

![](https://i.loli.net/2019/05/06/5ccfe43c3388f.gif)

在杨辉三角中，每个数是它左上方和右上方的数的和。

```
示例:

输入: 3
输出: [1,3,3,1]
进阶：

你可以优化你的算法到 O(k) 空间复杂度吗？
```

优化空间复杂度，需要牺牲一定的时间复杂度，可以使用递归实现。

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

> 执行用时 : 8 ms, 在Pascal's Triangle II的C++提交中击败了95.90% 的用户 内存消耗 : 9.2 MB, 在Pascal's Triangle II的C++提交中击败了5.14% 的用户


---

# 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/119.pascals_triangle_ii.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.
