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 Subtract3
From15
And we get12
, which is positive. Let's try to subtract more. Well, we shift3
To the left by1
Bit and we get6
. Subtracting6
From15
Still gives a positive result. Well, we shift again and get12
. We Subtract12
From15
And it is still positive. We shift again, obtaining24
And we know we can at the most subtract12
. Well, since12
is obtained by shifting3
To the left twice, we know it is4
Times of3
. How does we obtain this4
? Well, we start from1
and shift it to left twice at the same time. We add4
To 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:
divisor = 0
;
dividend = INT_MIN
and 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