2015年6月8日星期一

Divide Two Integers leetcode

Divide two integers without using multiplication, division and mod operator.
If it is overflow, return MAX_INT.   时间复杂 O(logn)
public class Solution {
    public int divide(int dividend, int divisor) {
        if (dividend == 0 || divisor == 0) {
            return 0;
        }
        if (dividend == Integer.MIN_VALUE && divisor == -1) {
            return Integer.MAX_VALUE;
        }
        boolean isNeg = (dividend < 0 && divisor >0) || (dividend >0 && divisor < 0);
        long a = Math.abs((long) dividend);
        long b = Math.abs((long) divisor);
        if (b > a) {
            return 0;
        }
        int result = 0;
        long pow = 0;
        long sum = 0;
        while (a >= b) {
            pow = 1;
            sum = b;
            while (sum + sum < a) {
                sum += sum;
                pow += pow;
            }
            a -= sum;
            result += pow;
        }
        if (isNeg) {
            return -result;
        } else {
            return result;
        }
    }
}

没有评论:

发表评论