30/34 test cases passed. Why does it fails for higher values?


  • 0
    D

    class Solution {
    public:
    vector<int> getRow(int rowIndex) {
    int i,c=1;
    long long int x;
    vector<int>result;
    rowIndex+=1;
    for(i=1;i<=rowIndex;i++)
    {
    result.push_back(c);
    x=c*(rowIndex-i);
    c=x/i;
    }
    return result;
    }
    };`


  • 0
    N

    Long long type is still overflowed.


  • 0
    D

    I tried unsigned long long int too. It still doesnt work. How to solve the issue?


  • 0

    the factorial will be too large. you can try: x = x * (rowIndex-i) /i; result.push_back(x). Hope this helps


  • 0
    E

    Hello and whoever are have this kind of problems:

    Simply change the data type to double will prevent the overflow and solve the problem. I modified your code and please try:

    class Solution {
    public:
    vector<int> getRow(int rowIndex) {
    double i,c=1;
    double x;
    vector<int>result;
    rowIndex+=1;
    for(i=1;i<=rowIndex;i++)
    {
    result.push_back(c);
    x=c*(rowIndex-i);
    c=x/i;
    }
    return result;
    }
    };
    

Log in to reply
 

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