Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- I can use two pointers to make this happen,
- The ith entry of the row contains 'i' elements
- the first and last entries of the list is '1'
- 1 -> 1
- 2 -> 2
- 3 -> 3
- 4 -> 4
- */
- class Solution {
- public:
- vector<vector<int>> generate(int numRows) {
- // Create the triangle
- vector<vector<int>> pascal;
- // Iterate over the triangle to fill it
- for(int k = 0; k < numRows; k++) {
- // Create a new row, with first and last entry as 1
- vector<int> row = vector<int>(k+1, 1);
- // push the row
- pascal.emplace_back(row);
- // If length is greater than 2
- if(k >= 2) {
- // Use two pointers to fill the array,
- // left will start for the 0th index, right will go upto the last index
- int l = 0, r = 1;
- while(r != k) {
- pascal[k][l+1] = pascal[k-1][l] + pascal[k-1][r];
- l++;
- r++;
- }
- }
- }
- return pascal;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement