[Leetcode] (Python): 029-divide, integers

Source: Internet
Author: User
Tags abs

Source of the topic:

https://leetcode.com/problems/divide-two-integers/

Test Instructions Analysis:

No multiplication, division, and MOD operations are used to achieve a division. Returns the maximum value of int if the value exceeds the int type.

Topic Ideas:

Initially, there are two practices.

① the process of simulating division, starting from the high side, not enough to move one right first. This method first takes the number of each digit first, because the maximum int type, so the input length is not more than 12 bits. The next step is to simulate the division process.

② uses the left shift operation to implement the departure process. Moving a number to the left equals a number x2, taking a tmp = divisor, so the divisor TMP keeps moving left until it is greater than dividend dividend, and then gets dividend-tmp, repeating the process.

Code (Python):

1 classsolution (object):2     defDivide (self, dividend, divisor):3         """4 : Type Dividend:int5 : Type Divisor:int6 : Rtype:int7         """8Ispositive =True9         ifDividend > 0 andDivisor <0:TenIspositive =False One         ifDividend < 0 andDivisor >0: AIspositive =False -Dividend = ABS (dividend);d ivisor =ABS (DIVISOR) -         ifDividend <Divisor: the             return0 -num = [1,10,100,1000,10000,100000,1000000,10000000,100000000,1000000000] -i = 9 -Newdividend = [] +          whileI >=0: -TMP =0 +              whileDividend >=Num[i]: ATMP + = 1;dividend-=Num[i] atNewdividend.append (TMP); I-= 1 -TMPM = 0; Ans = 0; i =0 -          whileI < 10: -              whileTMPM <Divisor: -                 ifI > 9: -                      Break inj = 0; t =0 -                  whileJ < 10 andTMPM! =0: toT + = TMPM; J + = 1 +TMPM = t + newdividend[i]; i + = 1 -                 ifTMPM <Divisor: thej = 0; t =0 *                      whileJ < 10 andAns! =0: $T + = ans; J + = 1Panax NotoginsengAns =T -             ifTMPM >=Divisor: theK =0 +                  whileTMPM >=Divisor: ATMPM-= divisor; K + = 1 thej = 0; t =0 +                  whileJ < 10 andAns! =0: -T + = ans; J + = 1 $Ans = t +k $         ifispositive: -             ifAns > 2147483647: -                 return2147483647 the             returnans -         ifAns >= 2147483648:Wuyi             return-2147483648 the         return2 Oans -                 
Simulation Process
1 classsolution (object):2     defDivide (self, dividend, divisor):3         """4 : Type Dividend:int5 : Type Divisor:int6 : Rtype:int7         """8Ispositive =True9         ifDividend > 0 andDivisor <0:TenIspositive =False One         ifDividend < 0 andDivisor >0: AIspositive =False -Dividend = ABS (dividend);d ivisor =ABS (DIVISOR) -         ifDividend <Divisor: the             return0 -TMP =Divisor -Ans = 1 -          whileDividend >=tmp: +TMP <<= 1 -             ifTMP >Dividend: +                  Break AAns <<= 1 atTMP >>= 1 -NaNs = ans + self.divide (Dividend-tmp,divisor) -         ifispositive: -             ifAns > 2147483647: -                 return2147483647 -             returnNaNs in         ifAns >= 2147483648: -             return-2147483648 to         return2 ONaNs +                 
Move left

Reprint Please specify source: http://www.cnblogs.com/chruny/p/4893254.html

[Leetcode] (Python): 029-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.