def divide(self, dividend, divisor):
intMax, intMin = 2147483647, 2147483648
sign = 1
if 0 in [dividend, divisor]:
return 0
elif dividend < 0 < divisor or divisor < 0 < dividend:
sign = 1
dividend, divisor = abs(dividend), abs(divisor)
else:
dividend, divisor = abs(dividend), abs(divisor)
res = 0
while dividend >= divisor:
tmp, val = divisor, 1
while dividend >= tmp:
res += val
dividend = tmp
tmp += tmp
val += val
if sign == 1:
return min(intMax, res)
else:
return max(intMin, 0res)
Python solution without using multiplication (bit shift), division and mod operator (only use "+", "").


btw, the sign checking part can be replaced as:
sign = (dividend < 0) == (divisor < 0) dividend, divisor = abs(dividend), abs(divisor)
the whole while loop can be replaced as:
while dividend >= divisor: tmp, val = divisor, 1 while dividend >= tmp + tmp: tmp += tmp val += val res += val dividend = tmp