[Leetcode] Divide integers

Source: Internet
Author: User

In this problem, we is asked to divide and integers. However, we is not allowed to use division, multiplication and mod operations. So, what else can is use? Well, bit manipulations.

Let's look at a example and see how bit manipulation might help.

Suppose we want to divide theBy3, so theIs dividend and3is divisor. Well, division simply requires us to find what many times can we subtract the divisor from the the the dividend without making The latter negative. Let ' s get started. We Subtract3From theAnd we get A, which is positive, so let's try to subtract more. Well, weShift 3To the left by1Bit and we get6. Subtracting6From theStill gives a positive answer. Well, we shift again and get A. We Subtract AFrom theAnd the answer is still positive. We shift again, obtaining -And we know we can at the most subtract A. Well, since Ais obtained by shifting3To the left twice, we know it is4Times of3. How does we obtain this4? Well, we start from1and shift it to left twice, at the same time. We add4to an answer. In fact, the above process was like3 * 4 + 3. We now get part of the quotient (4) with a remainder3.

Then we repeat the above process again. We Subtract divisor 3 from the remaining dividend 3 and obtain 0. We know we are done. No shift happens, so we simply add 1 << 0 to the answer.

Now we had had the full algorithm to perform division.

According to the problem statement, we need to handle some exceptions, such as flow.

Well, the cases may cause overflow:

    1. divisor = 0;
    2. dividend = int_min and divisor =-1 (because abs (int_min) = ABS (Int_max) + 1).

Of course, we also need to take the sign into account, which are relatively easy.

Putting things together, we have the following code.

1     intDivideintDividend,intdivisor) {2         if(!divisor | | (Dividend = = Int_min && divisor = =-1))3             returnInt_max;4         Long LongDVD =Labs (dividend);5         Long LongDVS =Labs (divisor);6         intSign = (Dividend >0) ^ (Divisor >0) ? -1:1;7         intres =0;8          while(DVD >=DVS) {9             Long LongTMP = DVS, multiple =1;Ten              while(DVD >= (TMP <<1)) { OneTMP <<=1; AMultiple <<=1; -             } -DVD-=tmp; theRes + =multiple; -         } -         returnSign *Res; -}

[Leetcode] Divide integers

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.