python:
class Solution:
def generate(self, numRows):
"""
:type numRows: int
:rtype: List[List[int]]
"""
res = []
arr = [1]*numRows
for i in range(numRows):
coarr = arr.copy()
for j in range(1,i):
k = coarr[j-1] + coarr[j]
arr[j] = k
res.append(arr[:i+1])
return res
c++:
class Solution {
public:
vector<vector<int>> generate(int numRows) {
vector<int> arr;
vector<vector<int>> res;
for (int i=0; i<numRows; i++){
for (int j=0; j<=i; j++){
arr.push_back(1);
}
res.push_back(arr);
arr.clear();
}
if (numRows<=2)
return res;
for (int i=2; i<numRows; i++){
for (int j=1; j<i; j++){
res[i][j] = res[i-1][j-1]+res[i-1][j];
}
}
return res;
}
};