Medium
刚看题目第一反应直接使用python3中//
求商并向下去整操作,商为负数情况需处理一下,但是题目要求不能使用除法运算符。过!
没有想法,学习一波大佬解法,获得新思路
A / B = C
=> 找到一个整数T,使得B的T倍最接近A但是不超过A
位运算
解题过程需考虑边界条件(\([-2^{31}, 2^{31} - 1]\)),以及除数为0的情况
python3
class Solution:
def divide(self, dividend: int, divisor: int) -> int:
if divisor == 0:
return0
if dividend == (0 - (2 << 30)) and divisor == -1:
return (2 << 30) - 1
if (dividend > 0 and divisor > 0) or (dividend < 0 and divisor < 0):
tag = 1
else:
tag = -1
dividend = abs(dividend)
divisor = abs(divisor)
res = dividend
c = 0
while res >= divisor:
a = 0
while res - (divisor = 0:
res = res - (divisor << a)
a += 1
if a >= 0:
c += (2 << a - 1) - 1
return c if tag == 1 else 0 - c