Share my 3ms simple solution


  • 0
    V
     class Solution {
        public:
            vector<int> getRow(int rowIndex) {
                vector<int> result;
                for(int i=0;i<=rowIndex;++i){
                    int last=0;
                    for(int j=0;j<i;++j){
                        int temp_int=result[j]+last;
                        last=result[j];
                        result[j]=temp_int;
                    }
                    result.push_back(1);
                }
                return result;
            }
        };
    

    The idea comes from the math of combintion:P(n,k)=P(n-1,k)+P(n-1,k-1)
    so we create the Triangle from top to bottom,so every row is one longer the the previous.


Log in to reply
 

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