Divide-integers without using multiplication, division and mod operator.
If It is overflow, return max_int.
Solution:
1 Public classSolution {2 Public intDivideintDividend,intdivisor) {3 if(divisor==0)returnInteger.max_value;4 if(dividend==0)return0;5 if(divisor==1)returndividend;6 if(Dividend==integer.min_value && Divisor==-1)returnInteger.max_value;7 8 BooleanNeg =false;9 //Change to all negtive number.Ten if(dividend>0){ OneDividend =-dividend; ANEG =!neg; - } - if(divisor>0){ theDivisor =-Divisor; -NEG =!neg; - } - + intMindivisor = Integer.min_value >> 1; - intCur =Divisor; + intval = 1; A while(Cur>=mindivisor && cur>dividend) { atCur <<= 1; -Val <<= 1; - } - - intres = 0; - while(dividend<=divisor) { in while(cur<dividend) { -Cur >>= 1; toVal >>= 1; + } -Dividend-=cur; theRes + =Val; * } $ Panax Notoginseng if(neg)return-Res; - Else returnRes; the } +}
Leetcode-divide integers