# My accepted python code, any suggestion on improving it?

• ``````class Solution:
# @return an integer
def divide(self, dividend, divisor):
# determine whether the result is positive or negative
isPositive=True
if (dividend<0 or divisor<0):
if (dividend<0 and divisor>0) or (dividend>0 and divisor<0):
isPositive=False
dividend=-dividend if dividend<0 else dividend
divisor = -divisor if divisor<0 else divisor
# deal with the case when divisor is zero
if not divisor:
return
# deal with the case when dividend is smaller than divisor
if dividend<divisor:
return 0
# double the divisor to speed up the counting. record the time and divisor pair in a queue
queue,res,tmp,dividend,divisor=[[1,divisor]],1,1,dividend-divisor,divisor+divisor
while dividend>=divisor:
tmp+=tmp
queue.append([tmp,divisor])
res+=tmp
dividend-=divisor
divisor+=divisor
# scan through the queue reversely to divide the residual
for ii in range(len(queue)-1,-1,-1):
while dividend>=queue[ii][1]:
dividend-=queue[ii][1]
res+=queue[ii][0]
if not isPositive:
res=-res
return res
``````

The code looks chunky. any suggestions would be appreciated.

• nice solution.

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.