Why not use the def of combination numbers?


  • 0
    A

    as C_n^k = C_n^(k-1) * (n-k+1)/k;

    the only thing is when computing C_n^(k-1) * (n-k+1)/k, we may suffer overflow.

    int calc(int x, int y, int z){
         return (x/z)*y + (x%z)*y/z;
    }
    
    vector<int> getRow(int rowIndex) {
        std::vector<int> vec;
        vec.push_back(1);
    
        for(int k = 1; k <= rowIndex; ++k){
            vec.push_back(calc(vec[k-1], (rowIndex-k+1), k));
        }
        return vec;
    }

Log in to reply
 

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