250x250
반응형
Notice
Recent Posts
Recent Comments
Link
N
(Leet Code c++)Pascal's Triangle(2) 본문
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
반응형
'Leet Code 알고리즘' 카테고리의 다른 글
(Leet Code c++)Best Time to Buy and Sell Stock(2) (0) | 2021.07.16 |
---|---|
(Leet Code c++)Best Time to Buy and Sell Stock (0) | 2021.07.16 |
(Leet Code c++)Pascal's Triangle (0) | 2021.07.15 |
(Leet Code c++)Path Sum (0) | 2021.07.15 |
(Leet Code c++)Minimum Depth of Binary Tree (0) | 2021.07.15 |