Topic
divide two integers
Dividing two integers requires the multiplication, division, and MOD operators not to be used.
If overflow, returns 2147483647
.
Sample Example
Given divisor = 100
, divisor = 9
, return11
Solving
15% pass rate, subtraction, bitwise operation? I don't know how to do that.
Law one: The use of subtraction, time-out, manual removal of some of the situation is too rogue.
Public classSolution {/** * @paramDividend The dividend *@paramdivisor the divisor *@returnThe result*/ Public intDivideintDividend,intdivisor) { //Write Your code here if(Dividend = = 0) return0; if(Dividend = =divisor)return1; intCount = 0; intFlag1 = 1; intFlag2 = 1; if(Dividend = = Integer.min_value && divisor ==1) returndividend; if(Dividend = = Integer.min_value && Divisor = =-1) return2147483647; if(dividend<0) {Flag1=-1; Dividend= -dividend; } if(divisor<0) {Flag2=-1; Divisor= -Divisor; } //dividend = 2147483647; //divisor = 2; if(Divisor = = 1) returnDividend*flag1*Flag2; while(Dividend >=divisor) {Dividend-=Divisor; Count+=1; } returnCount*flag1*Flag2; }}
Java Code
Law II: Batch subtraction, reference blog, but also the supermarket, the divisor is 1 time out, is 1 when I deal with alone, is 2 time out
Public classSolution {/** * @paramDividend The dividend *@paramdivisor the divisor *@returnThe result*/ Public intDivideintDividend,intdivisor) { //Write Your code here if(Dividend = = 0) return0; if(Dividend = =divisor)return1; intCount = 0; intFlag1 = 1; intFlag2 = 1; if(Dividend = = Integer.min_value && divisor ==1) returndividend; if(Dividend = = Integer.min_value && Divisor = =-1) return2147483647; if(dividend<0) {Flag1=-1; Dividend= -dividend; } if(divisor<0) {Flag2=-1; Divisor= -Divisor; } //dividend = 2147483647; //divisor = 2; if(Divisor = = 1) returnDividend*flag1*Flag2; while(Dividend >=divisor) { intsum =Divisor; intCount1 = 1; while(Sum + sum <=dividend) {Count1+=Count1; Sum+=sum; } Dividend-=sum; Count+=Count1; } returnCount*flag1*Flag2; }}
Java Code
Method three: Using bit arithmetic
Reference blog, feel and law two very much like, I put two in the weight loss change bit operation, also run timeout, I do not understand, directly with his program can pass.
Public classSolution {/** * @paramDividend The dividend *@paramdivisor the divisor *@returnThe result*/ Public intDivideintDividend,intdivisor) { //Write Your code here if(Divisor ==0) returnInteger.max_value; if(Divisor = =-1 && Dividend = =integer.min_value)returnInteger.max_value; intCount = 0; LongPdividend = Math.Abs ((Long) dividend); LongPdivisor = Math.Abs ((Long) divisor); while(Pdividend >=pdivisor) { intCount1 = 0; while((PDIVISOR<<COUNT1) <=pdividend) {Count1++; } Count+ = 1<< (count1-1); Pdividend-= (pdivisor<< (count1-1)); } if(Dividend >0 && divisor >0 | | dividend<0 && divisor<0) returncount; Else return-count; }}
Java Code
The bit operations in the blog can also be changed to subtraction, that is, I do the subtraction is to pass, can not understand
Public classSolution {/** * @paramDividend The dividend *@paramdivisor the divisor *@returnThe result*/ Public intDivideintDividend,intdivisor) { //Write Your code here if(Divisor ==0) returnInteger.max_value; if(Divisor = =-1 && Dividend = =integer.min_value)returnInteger.max_value; intCount = 0; LongPdividend = Math.Abs ((Long) dividend); LongPdivisor = Math.Abs ((Long) divisor); while(Pdividend >=pdivisor) { intCount1 = 1; Longsum =Pdivisor; while(sum + sum) <=pdividend) {Count1+=Count1; Sum+=sum; } Count+=Count1; Pdividend-=sum; } if(Dividend >0 && divisor >0 | | dividend<0 && divisor<0) returncount; Else return-count; }}
Java Code
Lintcode Medium title: Divide Two integers two number division