首页 🍒LeetCode,🐶算法

题目

VbMLf7lkaT.png

思路

思路借鉴:LeetCode题解

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);
    }
};



文章评论