sleepyotter
Pascal's Triangle 본문
1. 2차원 vector 생성
2. numRows에 맞춰 vector 생성 후 넣기
3. 3이하일 경우 계산하지 않고 반환
4. 3 이상일 경우 계산한 후 반환
class Solution {
public:
vector<vector<int>> generate(int numRows) {
//init
vector<vector<int>> ret;
for (int i = 0; i < numRows; ++i)
{
vector<int> v(i+1,0);
v[0] = 1;
v[i] = 1;
ret.push_back(v);
}
if (numRows < 3)
return ret;
for (int i = 2; i < numRows; ++i)
{
for (int j = 1; j < ret[i].size()-1; ++j)
{
ret[i][j] = ret[i - 1][j - 1] + ret[i - 1][j];
}
}
return ret;
}
};
'프로그래밍 > 알고리즘+코딩테스트' 카테고리의 다른 글
LeetCode/141.Linked List Cycle (0) | 2022.02.14 |
---|---|
[카카오]/추석 트래픽 (0) | 2022.02.07 |
Convet Sorted Array To Binary Search Tree (0) | 2021.09.24 |
Maximum Depth of Binary Tree (0) | 2021.09.23 |
Symmetric Tree (0) | 2021.09.23 |