Lintcode Medium title: Divide Two integers two number division

Source: Internet
Author: User
Tags lintcode

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&LT;&LT;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

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.