解题思路
python 代码
class Solution:
def divide(self, dividend: int, divisor: int) -> int:
# /** 除数为零就返回-1 按照测试样例的要求写的*/
if divisor==0:
return -1
if dividend==0:
return 0
# /** -2147483648, -1 这个测试样例的确没想到,结果翻车了*/
if dividend==-2147483648 and divisor==-1:
return 2147483647
sign = 1
if (dividend>0 and divisor<0) or (dividend<0 and divisor>0):
sign = -1
# /** div_count 是当前divisor_tmp相对于divisor的倍数 */
res=0
div_count=1
# /** 因为值溢出之后边界问题处理太繁琐了,直接将数值转为long省去麻烦 */
dividend_tmp= abs(dividend)
divisor_tmp= abs(divisor)
# /** 按标准的二分查找代码模板写的 */
while dividend_tmp >= divisor_tmp:
dividend_tmp-= divisor_tmp
res+= div_count
if dividend_tmp< abs(divisor):
break
# divisor_tmp无法倍增时,就将其初始化为divisor绝对值,重新开始下一轮倍增*/
if dividend_tmp- divisor_tmp< divisor_tmp:
divisor_tmp= abs(divisor)
div_count=1
continue
# 不断倍增divisor_tmp直到和dividend_tmp一样大*/
divisor_tmp+= divisor_tmp
div_count+= div_count
if sign==-1:
return -res
return res
s = Solution()
res = s.divide(10,3)
print('.....',res)