[Leetcode] [Python]29:divide integers

Source: Internet
Author: User

#-*-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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.