Skip to content

Commit

Permalink
✨ solve 118
Browse files Browse the repository at this point in the history
  • Loading branch information
Potato Chip committed Nov 12, 2021
1 parent 0e33644 commit f8f62de
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions 118.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// https://leetcode.com/problems/pascals-triangle/

#include <bits/stdc++.h>
using namespace std;

class Solution {
public:
vector<vector<int>> generate(int numRows) {
vector<vector<int>> triangle(numRows);

vector<int> row1(1);
row1[0] = 1;

triangle[0] = row1;

for (int i = 1; i < numRows; ++i) {
vector<int> row(i + 1);

for (int j = 0; j <= i; ++j) {
if (j == 0 || j == i) {
row[j] = 1;
} else {
row[j] = triangle[i - 1][j - 1] + triangle[i - 1][j];
}
}

triangle[i] = row;
}

return triangle;
}
};

0 comments on commit f8f62de

Please sign in to comment.