(Leetcode) Divide two integers (Java) idea explanation and realization __ Mathematics

Source: Internet
Author: User
Tags abs

This article will introduce the divide two integers and the Java implementation in detail.

The topics are as follows:

Divide two integers without using multiplication, division and mod operator. (The division of integers is implemented without multiplication, division, and remainder operations)

If It is overflow, return max_int. (integer overflow returns the maximum number of integers)

Topic Analysis:

1. Divisor/divisor = quotient (ignoring remainder) => is divisor = divisor * quotient.

2. Quotient (any integer) can be expressed as: A_0*2^0+a_1*2^1+...a_i*2^i+...+a_n*2^n.

3. The left-shift operation in Java << is equal to a number multiplied by 2, and the right shift is equivalent to dividing by 2.

4. We allow the divisor to move left until it is greater than a number before the divisor, for example, 28/3, we perform a three left shift operation to make the 3*2*2*2=3*8=24<28 (note four times left move operation get 3*2^4=48>28). Record the 2*2*2=2^3=8.

5. We let 28 minus 24 get 4, then calculate 4/3 like step fourth, then 3*2^0=3<4. Record the 2^0=1.

6. Since the 4-3=1 is less than divisor 3, the calculation is stopped and the value of each round is added, in this case 8+1=9, remember to the quotient (ie 28/3=9).

So far, the subject of the program has been introduced, next to note the data left and the integer absolute value of the boundary problem.

7. Converts the input int (32-bit) number to a long (64-bit) type.

8. Consider the special circumstances of the min_value/-1.

The Java implementation code is as follows:

static int divide (int dividend, int divisor) {
        int result=0;
        if (divisor = = 0)//divisor is 0, returns the maximum value return
            integer.max_value;
        if (dividend = = Integer.min_value && divisor = = 1) {return
            ~dividend;
        }
        Long Dividend1 = Math.Abs ((long) dividend);
        Long Divisor1 = Math.Abs ((long) divisor);

        while (Dividend1 >= divisor1)//When the divisor is greater than the divisor, perform the displacement operation
        {
            int shiftnum = 0;
            while (Dividend1 >= divisor1<<shiftnum) {
                shiftnum++;//record number of left shifts (1 more than actual)
            } result
            + = 1<< (shiftnum-1);
            Dividend1-= divisor1<< (shiftnum-1);//Update dividend value
        }
        if (dividend>0 && divisor>0) | | (dividend<0 && divisor<0)) Calculate positive negative return results
            ;
        else
            Return-result;
    }
You are welcome to leave a message to discuss.
Related Article

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.