N

(Leet Code c++)Pascal's Triangle(2) 본문

Leet Code 알고리즘

(Leet Code c++)Pascal's Triangle(2)

naeunchan 2021. 7. 15. 14:56
728x90
반응형

119. Pascal's Triangle II

Easy

1536230Add to ListShare

Given an integer rowIndex, return the rowIndexth (0-indexed) row of the Pascal's triangle.

In Pascal's triangle, each number is the sum of the two numbers directly above it as shown:

 

Example 1:

Input: rowIndex = 3 Output: [1,3,3,1]

Example 2:

Input: rowIndex = 0 Output: [1]

Example 3:

Input: rowIndex = 1 Output: [1,1]

 

Constraints:

  • 0 <= rowIndex <= 33
class Solution {
public:
vector<int> getRow(int rowIndex) {
vector<vector<int>> answer;

for(int i = 0; i <= rowIndex; i++){
vector<int> v(i + 1, 1);

for(int j = 1; j < v.size() - 1; j++){
v[j] = answer[i - 1][j - 1] + answer[i - 1][j];
}

answer.push_back(v);
}

return answer[rowIndex];
}
};
728x90
반응형