Leetcode 119 Solution

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

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

Solution

class Solution {
public:
    vector<int> getRow(int rowIndex) {
        vector<int> a;
        a.resize(rowIndex + 1);

        for (int i = 0; i <= rowIndex; i++)
        {
            if (i == 0)
                a[0] = 1;
            else
            {
                int lastval = 0;

                for (int j = 0; j <= rowIndex; j++)
                {
                    if (j == 0)
                        a[j] = 1, lastval = a[j];
                    else if (j < rowIndex)
                    {
                        int tmp = lastval + a[j];
                        lastval = a[j];
                        a[j] = tmp;
                    }
                    else
                        a[j] = 1;
                }
            }
        }

        return a;
    }
};