Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution:
- def divide(self, dividend: int, divisor: int) -> int:
- ans=0
- sign=(dividend<0)==(divisor<0)
- divisor,dividend=abs(divisor),abs(dividend)
- ```
- Here we will copy divisor to tsum and every time we double tsum and checks whether it
- has reached dividend or not.
- If it reaches dividend we add current quotient to our answer and will make new dividiend
- from old divident that has eaten by current quotient
- ```
- tsum=divisor
- while tsum<=dividend:
- q=1
- while tsum+tsum<=dividend:
- q=q+q
- tsum=tsum+tsum
- ans+=q
- dividend=dividend-tsum
- tsum=divisor
- ans=ans if sign else -ans
- ```
- Since question has said range is in given 32 bits.
- max 32 bit value is 2**31-1
- min 32 bit value is -2**31
- ```
- return min(max(-2**31, ans), 2**31-1)
Advertisement
Add Comment
Please, Sign In to add comment