Share my c++ implementation.


  • 0
    J
    vector<int> getRow(int rowIndex) {
        vector<int> ret(rowIndex+1);
        for(int i = 0; i <= rowIndex/2; i++){
            ret[i] = ret[rowIndex-i] = calcPascalNum(rowIndex, i);
        }
        return ret;
    }
    int calcPascalNum(const int row, int pos){
        if(pos == 0 || pos == row){
            return 1;
        }
        unsigned long ret = 1;
        for(unsigned int i = row; i > (row-pos); i--){
            ret *= i;
            ret /= (row - i + 1);
        }
        return (int)ret;
    }

Log in to reply
 

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