[Leetcode] [Python] Divide two Integers__python

Source: Internet
Author: User
The main effect of the topic

Division operations, but not the multiplication, division, and modulo operations provided by the programming language, that is, can only be achieved by addition and subtraction. ideas for solving problems

https://blog.csdn.net/qian2729/article/details/50528758 Code

Class Solution (object):
    def divide (self, dividend, divisor): ""
        : Type dividend:int:
        type divisor: int
        : Rtype:int "" "
        max_int = 2147483647
        sign = 1 if (Dividend > 0 and divisor > 0) or (dividend &L T 0 and Divisor < 0) else-1
        quotient = 0
        dividend = ABS (dividend)
        divisor = ABS (divisor) while
        divID End >= Divisor:
            k = 0
            tmp = divisor while
            dividend >= tmp:
                dividend = tmp
                quotient + = 1 &L t;< k
                tmp <<= 1
                k = 1
        Quotient  = sign * quotient
        if quotient > max_int:
            quotie NT = max_int return
        quotient
Summarize

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.