Divide integers Leetcode

Source: Internet
Author: User
Tags what bit

Title: Divide integers

Divide-integers without using multiplication, division and mod operator.

If It is overflow, return max_int.

Look at the idea of the great God in the discussion area:

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

Let's do a example and see what bit manipulations work.

Suppose we want to divide 15 3 by, so are and is 15 dividend 3 divisor . Well, division simply requires us to find what many times we can subtract the from the the the divisor dividend without making the c8/> negative.

Let ' s get started. We Subtract3From15And we get12, which is positive. Let's try to subtract more. Well, we shift3To the left by1Bit and we get6. Subtracting6From15Still gives a positive result. Well, we shift again and get12. We Subtract12From15And it is still positive. We shift again, obtaining24And we know we can at the most subtract12. Well, since12is 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 a answer (initialized to be0). In fact, the above process was like15 = 3 * 4 + 3. We now get part of the quotient (4) with a remainder3.

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

Now we had the full algorithm to perform division.

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

Well, the cases may cause overflow:

    1. divisor = 0;
    2. dividend = INT_MINand divisor = -1 (because abs(INT_MIN) = INT_MAX + 1 ).

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

Putting all these together, we have the following code.

ClassSolution {Publicint Divide (int dividend,int divisor) {if (!divisor | | (Dividend = = Int_min && divisor = =-1))return int_max; int sign = ((Dividend < 0) ^ (Divisor < 0))?-
                 
                  1: 
                  1; long long DVD = Labs (dividend); long long DVS = Labs (divisor); int res = 0; while (DVD >= DVS) {long long temp = DVS, multiple = 1; while (DVD >= (temp << 1)) {temp <<= 1; Multiple <<= 1;} DVD-= temp; Res + = multiple; } return sign = = 1? Res:-res;}};       
                 

1#include <iostream>2#include <limits>3 using namespacestd;5 classSolution {6  Public:7     intDivideintDividend,intdivisor)8     {9         intSign = ((Dividend >0) ^ (Divisor >0) ? -1:1);Ten         if(!divisor | | (dividend==int_min&&divisor==-1)) One             returnInt_max; A         Long LongdivID = Labs (dividend), Divis =Labs (divisor); -         Long Longres =0; -          while(divID >=Divis) the         { -             Long Longtemp = Divis,multi_time=1; -              while(divID >= (temp<<1)) -             { +Temp <<=1; -Multi_time <<=1; +             } AdivID-=temp; atRes + =Multi_time; -         } -         returnSign = =1? res:-Res; -     } - }; - intMain () in { - solution test; to     intres = Test.divide (0,1); +cout << Res <<Endl; -     return 0; the}

Divide integers Leetcode

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.