#-*-Coding:utf8-*-
‘‘‘
__author__ = ' [email protected] '
29:divide integers
https://oj.leetcode.com/problems/divide-two-integers/
Divide-integers without using multiplication, division and mod operator.
If It is overflow, return max_int.
===comments by dabay===
Determine the symbol first.
To do the French child:
A string depositary, a string that holds the remainder.
When calculating each quotient, the remainder is used to subtract the number, and if the remaining number is greater than 0, the quotient adds 1.
When you finally return, consider the overflow mentioned in the topic. (personal feeling that this requirement is meaningless)
‘‘‘
Class Solution:
# @return An integer
Def divide (self, dividend, divisor):
If dividend = = 0:
return 0
if (Dividend > 0 and Divisor < 0) or (dividend < 0 and divisor > 0):
quotient = "-"
Else
quotient = ""
Dividend = ABS (dividend)
DIVISOR = ABS (divisor)
Dividend = str (dividend)
i = 0
remainder = ""
While I < Len (dividend):
remainder = Int (str (remainder) + dividend[i])
quot = 0
While remainder >= divisor:
Remainder-= divisor
quot + = 1
quotient + = str (quot)
i + = 1
if int (quotient) > 2147483647:
Return 2147483647
elif Int (quotient) <-2147483648:
Return 2147483648
Else
return int (quotient)
def main ():
Sol = solution ()
Print Sol.divide (1,-1)
if __name__ = = "__main__":
Import time
Start = Time.clock ()
Main ()
Print "%s sec"% (Time.clock ()-start)
[Leetcode] [Python]29:divide integers