Problem

Divide two integers without using multiplication, division and mod operator.

If it is overflow, return 2147483647.

Example

Given dividend = 100 and divisor = 9, return 11.

Note

首先,分析溢出条件,设置符号位,然后取绝对值运算
原理如下,被除数a,除数b,设c等于b。
在b<=a时,令除数c每次乘以2,增大到小于等于被除数a的时候,c乘了几次,商里就会有一个2的几次方。如25 / 4,4 * 4 < 25,4 * 8 > 25,所以商里必定有一个4(2的2次方)存入temp,然后res += temp
然后被除数a减去c,继续check。a = 25 - 4 * 4 = 9,c = 4 * 2 = 8,所以商里必定有一个8 / 4 = 2(2的1次方)存入temp。此时a = 9 - 8 = 1,a < b,循环结束。res = 4 + 2 = 6,为所求。

再举一个例子:

13 / 3, a = 13, b = 3, c = 3:
c = 3, temp = 1; c = 6, temp = 2; c = 12; temp = 4;
c = 3, res = 4, a = 1, a < b, return res = 4.

Solution

public class Solution {
    public int divide(int dividend, int divisor) {
        long res = 0;
        boolean neg = (dividend < 0 && divisor > 0) || (dividend > 0 && divisor < 0);
        long a = Math.abs((long)dividend);
        long b= Math.abs((long)divisor);
        while (a >= b) {
            long c = b;
            long temp = 0;
            while (c <= a) {
                temp = temp == 0 ? 1 : temp << 1;
                c = c << 1;
            }
            c = c >> 1;
            a -= c;
            res += temp;
        }
        if (res >= Integer.MAX_VALUE) res = neg ? Integer.MIN_VALUE : Integer.MAX_VALUE;
        else if (neg) res = -res;
        return (int)res;
    }
}

看得懂的版本:

public class Solution {
    public int divide(int dividend, int divisor) {
        boolean isNeg = (dividend < 0 && divisor > 0) || (dividend > 0 && divisor < 0);
        if (dividend == 0) return 0;
        if (divisor == 0) return dividend > 0 ? Integer.MAX_VALUE: Integer.MIN_VALUE;
        long A = Math.abs((long)dividend); //long inside Math.abs
        long B = Math.abs((long)divisor);
        if (A < B) return 0;
        int res = 0;
        long ans = helper(A, B);
        if (ans >= Integer.MAX_VALUE) res = isNeg ? Integer.MIN_VALUE : Integer.MAX_VALUE; //distinguish corner cases
        else res = isNeg ? -(int)ans : (int)ans;
        return res;
    }
    public long helper(long A, long B) {
        if (A < B) return 0;
        long sum = B;
        long res = 1;
        while (sum+sum <= A) {
            sum += sum;
            res += res;
        }
        return res + helper(A-sum, B);
    }
}

综合一下

public class Solution {
    public int divide(int dividend, int divisor) {
        int sign = 1;
        if ((dividend < 0 && divisor > 0) || (dividend > 0 && divisor < 0)) sign = -1;
        long A = Math.abs((long)dividend);
        long B = Math.abs((long)divisor);
        if (A < B) return 0;
        long res = 0;
        while (A >= B) {
            long sum = B;
            long temp = 1;
            while (sum+sum < A) {
                sum += sum;
                temp += temp;
            }
            A -= sum;
            res += temp;
        }
        if (res >= Integer.MAX_VALUE) res = sign == 1 ? Integer.MAX_VALUE : Integer.MIN_VALUE;
        else res = res * sign;
        return (int)res;
    }
}

linspiration
161 声望53 粉丝