Leetcode 118 Solution

This article provides solution to leetcode question 118 (pascals-triangle)

https://leetcode.com/problems/pascals-triangle

Solution

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

        for (int i = 0; i < numRows; i++)
        {
            vector<int> a;

            if (i == 0)
            {
                a.push_back(1);
            }
            else
            {
                const vector<int>& b = res[i - 1];

                for (int j = 0; j < b.size(); j++)
                {
                    if (j == 0)
                        a.push_back(1);
                    else
                        a.push_back(b[j] + b[j - 1]);
                }

                a.push_back(1);
            }

            res.push_back(a);
        }

        return res;
    }
};