Simple C++ solution in O(k) time complexity and O(k) space complexity.The idea is to use the property of Binomial Co-efficients. C(n,r) = (n-r+1)/r * C(n,r-1).


  • 0
    P
        class Solution {
    public:
        vector<int> getRow(int row) {
            vector<int> out;
            out.push_back(1);
            long prev = 1;
            long curr;
            
            for(int i=1 ; i<=row/2 ; i++){
                curr = ((row-i+1)*prev)/i;
                out.push_back(curr);
                prev = curr;
            }
            for(int i = (row&1)?row/2:(row/2)-1 ; i>=0 ; i--){
                out.push_back(out[i]);
            }
            return out;
        }
    };
    

Log in to reply
 

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