# Pascal's Triangle

> Given numRows, generate the first numRows of Pascal's triangle.

> For example, given numRows = 5,
> Return

> ```
[
[1],
[1,1],
[1,2,1],
[1,3,3,1],
[1,4,6,4,1]
]
> ```

+ 第k层有k个元素
+ 每层第一个以及最后一个元素值为1
+ 对于第k（k > 2）层第n（n > 1 && n < k）个元素A[k][n]，A[k][n] = A[k-1][n-1] + A[k-1][n]

```c++
class Solution {
public:
vector > generate(int numRows) {
vector > vals;
vals.resize(numRows);

for(int i = 0; i < numRows; i++) {
vals[i].resize(i + 1);
vals[i][0] = 1;
vals[i][vals[i].size() - 1] = 1;
for(int j = 1; j < vals[i].size() - 1; j++) {
vals[i][j] = vals[i - 1][j - 1] + vals[i - 1][j];
}
}

return vals;
}
};
```

# Pascal's Triangle II

> Given an index k, return the kth row of the Pascal's triangle.

> For example, given k = 3,
> Return [1,3,3,1].

```c++
class Solution {
public:
vector getRow(int rowIndex) {
vector vals;

vals.resize(rowIndex + 1, 1);

for(int i = 0; i < rowIndex + 1; ++i) {
for(int j = i - 1; j >= 1; --j) {
vals[j] = vals[j] + vals[j - 1];
}

}

return vals;
}
};
```