Why my solution get TLE?


  • 0
    Z

    I think this solution is correct, why I get LTE?

    int divide(int dividend, int divisor) {
    if (divisor == 1)
       return dividend;
    
    if (dividend == MIN_INT && divisor == -1)
        return MAX_INT;
    
    bool sign = false;
    if (dividend >= 0 ^ divisor >= 0)
        sign = true;
    long long end, sor;
    
    end = abs(dividend);
    sor = abs(divisor);
    
    long long res = 0;
    long long power, temp;
    while (end >= sor) {
        power = 1;
        temp = sor;
        while ((temp << 1) <= end) {
            power = power << 1;
            temp = temp << 1;
        }
        res += power;
        end -= temp;
    }
    
    if (sign)
        return -res;
    return res;
    

    }


  • 1
    W

    Change

    end = abs(dividend);
    sor = abs(divisor);
    

    into

    end = abs((long long)dividend);
    sor = abs((long long)divisor);
    

    abs(0x80000000) is still 0x80000000 since dividend is a 32bit number.


  • 0
    Z

    Thanks,It works.


Log in to reply
 

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