Question Link:
https://leetcode.com/problems/divide-two-integers/
Solution
Use bit shift operation to simulate x2
or /2
Implementation
[-]
Python code accepted by LeetCode OJ
class Solution(object):
def divide(self, dividend, divisor):
"""
:type dividend: int
:type divisor: int
:rtype: int
"""
#python 3 have removed this constant
MAX_INT = 2147483647
MIN_INT = -2147483648
if divisor == 0 or (divisor == -1 and dividend == MIN_INT):
return MAX_INT
elif divisor == 1:
return dividend
elif divisor == -1:
return -dividend
positive = True
if dividend < 0:
dividend = -dividend
positive = not positive
if divisor < 0:
divisor = - divisor
positive = not positive
if dividend < divisor:
return 0
res = 0
while dividend >= divisor:
m = 1
d = divisor
while (d << 1) < dividend:
d = d << 1
m = m << 1
res += m
dividend -= d
if positive:
return res
else:
return -res