LC* 29. 两数相除

2022-01-13 15:02:14 浏览数 (1)

题目

思路

思路借鉴:LeetCode题解

代码语言:javascript复制
class Solution {
public:
    int div(long a, long b) {
        long c = b;
        if (a < b) return 0;
        long count = 1;
        while (b   b <= a) {
            count <<= 1;
            b <<= 1;
        }
        return count   div(a - b, c);
    }

    int divide(int dividend, int divisor) {
        long a = dividend, b = divisor;
        int jud = 0;
        if (a == -2147483648) {
            if (b == 1) return -2147483648;
            else if (b == -1) return INT_MAX;
        }
        if ((a > 0 && b < 0) || (a < 0 && b > 0)) jud = 1;
        a = a > 0 ? a : -a;
        b = b > 0 ? b : -b;
        if (jud == 1) return -div(a, b);
        return div(a, b);
    }
};
ode

0 人点赞