classSolution: # @return an integer defdivide(self, dividend, divisor):
if0 == divisor or0 == dividend: return0 elif1 == divisor: return dividend elif -1 == divisor: # -(-2147483648) does not fit in 32 bits, return MAX_INT return2147483647if -2147483648 == dividend else -dividend
positive = 1 if (dividend >0and divisor < 0) or (dividend < 0and divisor > 0): positive = -1 dividend = dividend if dividend > 0else -dividend divisor = divisor if divisor > 0else -divisor
result = 0 dividend_now = dividend while dividend_now >= divisor: total = divisor sub_result = 1 whileTrue: total = total << 1 sub_result = sub_result << 1## not += 2 if total > dividend_now: total = total >> 1 sub_result = sub_result >> 1 break elif total == dividend_now: break result += sub_result dividend_now -= total return result if1 == positive else -result