Lintcode-medium-divide integers

Source: Internet
Author: User
Tags lintcode

Divide-integers without using multiplication, division and mod operator.

If It is overflow, return2147483647

Given dividend = 100 and divisor = 9 , return 11 .

 Public classSolution {/**     * @paramDividend The dividend *@paramdivisor the divisor *@returnThe result*/     Public intDivideintDividend,intdivisor) {        //Write Your code here                if(Divisor = = 0)            return2147483647; if(Dividend = = 0)            return0; if(Divisor = = 1)            returndividend; if(Dividend = = Integer.min_value && Divisor = =-1) {            returnInteger.max_value; }                BooleanNeg = (Dividend < 0 && divisor > 0) | | (Dividend > 0 && Divisor < 0); Longnum = Math.Abs (Long) dividend); LongDen = Math.Abs ((Long) divisor); Longresult = 0;  while(Num >=den) {            intShift = 0;  while(Num >= (den <<shift)) Shift++; Num-= den<< (shift-1); Result+ = 1 << (shift-1); }        if(neg) {if(Result > Math.Abs (Long) integer.min_value))return2147483647; Else                    return(int) (-result); }        Else{            if(Result > (Long) integer.max_value)return2147483647; Else                return(int) result; }    }}

Lintcode-medium-divide integers

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.