"Leetcode-Interview algorithm classic-java Implementation" "029-divide two integers (divide two integers)"

Source: Internet
Author: User

"029-divide two integers (divide two integers)" "leetcode-Interview algorithm classic-java Implementation" "All topics Directory Index" Original Question

Divide-integers without using multiplication, division and mod operator.
If It is overflow, return max_int.

Main Topic

The result of dividing two integers without using division, multiplication, and taking, returns the largest integer if there is an overflow.

Thinking of solving problems

Any integer can be represented as a linear combination of a set of bases at the base of a power of 2, namely num=a_0*2^0+a_1*2^1+a_2*2^2+...+a_n*2^n. Based on the above formula and the shift left one is equivalent to multiplying by 2, we first let the divisor shift left until it is greater than dividend to get a maximum base. Then each time we try to subtract the base, if we can add 2^k to the result, then the base continues to move the iteration right until the base is 0. The time complexity is O (log (n)) because the iteration number of this method is known to exceed the result by a power of 2.

Code Implementation

Algorithm implementation class

 Public classSolution { Public int Divide(intDividend,intDivisor) {//Divide overflow handling        if(Divisor = =0|| Dividend = = Integer.min_value && divisor = =-1) {returnInteger.max_value; }//Find sign bit        intSign = ((Dividend <0) ^ (Divisor <0)) ? -1:1;//Seek absolute value, to prevent overflow using long        LongDVD = Math.Abs ((Long) dividend);LongDVS = Math.Abs ((Long) divisor);//Record results        intresult =0;//dividend greater than divisor         while(DVD >= DVS) {//Record divisor            LongTMP = DVS;//size of the logger            LongMul =1; while(DVD >= (TMP <<1) {tmp <<=1; Mul <<=1; }//Subtract the value of the index times of the DVS closest to the DVD (value tmp)DVD-= TMP;//Correction Resultsresult + = Mul; }returnresult * SIGN; }}
Evaluation Results

  Click on the picture, the mouse does not release, drag a position, release after the new window to view the full picture.

Special Instructions Welcome reprint, Reprint please indicate the source "http://blog.csdn.net/derrantcm/article/details/47052683"

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

"Leetcode-Interview algorithm classic-java Implementation" "029-divide two integers (divide two 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.