Simple "0ms" C++ solution using O(k) space


  • -1
    D
    class Solution {
    public:
        vector<int> getRow(int rowIndex) {
            vector<vector<int> > rows {vector<int>(rowIndex + 1), vector<int>(rowIndex + 1)};
            rows[0][0] = 1;
            for (int i = 1; i <= rowIndex; ++i) {
                rows[i & 1][0] = 1;
                for (int j = 1; j < i; ++j) {
                    rows[i & 1][j] = rows[(i - 1) & 1][j - 1] + rows[(i - 1) & 1][j];
                }
                rows[i & 1][i] = 1;
            }
            return rows[rowIndex & 1];
        }
        
    };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.