파스칼의 삼각형을 주어진 인수의 크기에 맞게 그리는 문제입니다.
https://leetcode.com/problems/pascals-triangle
class Solution {
public:
vector<vector<int>> generate(int numRows) {
vector<vector<int>> ans;
vector<int> cur(1,1);
for(int i=1;i<=numRows;i++){
vector<int> next(i,1);
for(int j=1;j<i-1;j++){
next[j] = cur[j-1] + cur[j];
}
ans.push_back(next);
cur.swap(next);
}
return ans;
}
};
처음에는 규칙을 찾으려고 했다가 실패하고, 값이 누적되어 가는 것이 피보나치 수열 같아서 for문으로 구현했더니 쉽게 구할 수 있었습니다.