O(1) Space, Easiest to Understand, by using N choose K method.


  • 0
    E

    he entry in the nth row and kth column of Pascal's triangle is N choose k. As a reminder, n and k starts from 0.

    I wrote a simple function for N choose K and as you can see the solution is pretty straightforward.

    Please let me know if you have any questions.

    class Solution {
    private:
        double n_choose_k(int n, int k){
            double result = 1;
            for (int i = 1; i <= k; i++)
            {
                result *= n-i+1;
                result /= i;
            }   
            return result;
        }
    public:
        vector<int> getRow(int rowIndex) {
        vector<int> res;
        for (int i = 0; i< rowIndex+1; i++) res.push_back(n_choose_k(rowIndex,i));
         return res;    
        }
    };
    

Log in to reply
 

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