Divide integers Total Accepted: 26724 submissions: 167045 My Submissions
Question Solution
Divide-integers without using multiplication, division and mod operator.
If It is overflow, return max_int.
It is difficult to get ideas if the problem is not to refer to others ' answers. In general, it is the dichotomy. Binary search is a very familiar content, but really the relevant methods to the application of the time still feel slightly difficult.
Reference: http://www.cnblogs.com/panda_lin/p/divide_two_integers.html
This problem is a very good experience. Put the code on:
classSolution { Public: UnsignedLong LongNewdivide (unsignedLong LongDividend, unsignedLong Longdivisor) {unsignedLong Longtemp=Divisor,left; unsignedLong Longresult=1; if(Dividend <divisor)return 0; while(Temp <=dividend) { Left= Dividend-temp; Temp<<=1; if(Temp >dividend) Break; Elseresult<<=1; } returnResult +Newdivide (left, divisor); } intDivideintDividend,intdivisor) { if(Divisor = =0) Throw 1;//throw an error out BOOLPositive = (Dividend >=0&& divisor >0) || (Dividend <=0&& Divisor <0); unsignedLong Long_dividend = ABS (Long Long) dividend); unsignedLong Long_divisor = ABS (Long Long) divisor); Long LongTempresult=positive? Newdivide (_dividend, _divisor):-1*newdivide (_dividend, _divisor); if(ABS (Tempresult) >=Int_max) Tempresult= positive?int_max:int_min; returnTempresult; }};
Then learn the contents of the dichotomy:
Happyleetcode45:divide integers