soulmachine / leetcode

LeetCode题解,151道题完整版。
BSD 3-Clause "New" or "Revised" License
11.3k stars 3.43k forks source link

15.13 divide two integers, need to retrun INT_MAX when result is overflow #46

Open riveridea opened 9 years ago

riveridea commented 9 years ago

Leetcode has added the new case for this problem, when overflow occurs, need to return INT_MAX, the current code can NOT be accepted. I adjust the return code as this and accepted.

    long long ret =    ((dividend^divisor) >> 31)?(-multi): multi;

    if (ret > INT_MAX)
        return INT_MAX;
    else
        return ret;