LeetCode-29: do not use multiplication, division, and mod operator on how to solve the quotient of two numbers, the real interview met

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/qq_41885819/article/details/102718981

The subject at the beginning of this year, one of my colleagues when it came to the interview too, has come back to me Tucao, some question what I am not algorithms engineer interview, ask it? Haha ~
I believe a lot of people, if not always learn the words, probably will be forced to look ignorant, I am also one :). While these issues with work opportunities in practical use rarely, but for us to expand thinking, language and some understanding of the underlying logic operation, is very helpful.

Subject description:

Given two integers, dividend dividend and divisor divisor. The dividing the two requirements without using multiplication, division, and the mod operator. Return dividend
dividend divided by the divisor quotient divisor get.

Description:

  • Dividend and divisor are 32-bit signed integer.
  • Divisor is not zero.
  • We assume that the environment can only store 32-bit signed integer, which is the numerical range [-2 31 is 2 31 is - 1] In this problem, if the division result overflows, it returns 231--1.

For practical example, 57/4, the actual needs of the quotient is 14, and the remainder is 1.
A relatively easy way to think of is the direct subtraction using 57-4 = 53, and 53> 4; 53-4 = 49 and then again on until the difference out of the Save <4, at a time when the number of times to do subtraction, what we need quotient. This approach is very real, the biggest problem is efficiency. 57 The number of relatively small, if it is 999999/4, where the number of cycles or recursion is more uncomfortable.

Great God of online reference solution, using bit computing.
This operation is in the way we used the actual development is relatively small, so it is not easy to want, but mentioned it, that is, Whoa, whoa, I know I know. . .
First look at the code:

package com.leetcode.learning;

public class _0029Division {
    public int divide(int dividend, int divisor) {
        if (dividend == 0) {
            return 0;
        }
        if (dividend == Integer.MIN_VALUE && divisor == -1) {
            return Integer.MAX_VALUE;
        }
        boolean negative;
      //用异或来计算是否符号相异
        negative = (dividend ^ divisor) <0;
        long t = Math.abs((long) dividend);
        long d= Math.abs((long) divisor);
        int result = 0;
        for (int i=31; i>=0;i--) {
        	//找出足够大的数2^n*divisor
            if ((t>>i)>=d) {
            	//将结果加上2^n
                result+=1<<i;
              //将被除数减去2^n*divisor
                t-=d<<i;
            }
        }
        //符号相异取反
        return negative ? -result : result;
    }
}

More difficult to understand the fact that the internal part of the loop, we still have a good case study to 57/4.
First make a dividend bit arithmetic right shift bits cycle from 31 to 31 >> 0,57 left only a string of 0, and certainly smaller than the divisor, No, no, continue to cycle, so that the right position log reduction, occurs until the number 3, 57 = 7,7 >> 3> = 4 meets the requirements; dividend and replace 57-4 * 2 3 = 25, the same divisor, or 4, from the result becomes 2 0 3 = 8; was continued for 25 6,6 >> 2 => 4 = satisfactory; dividend continue to replace 25--4 * 2 2 = 9, the same divisor, or 4, the result becomes 8 + 8 from 2 2 = 12 ; continue 9 4,4 >> 1 => 4 = satisfactory; replace dividend 9 - 4 * 2 1 = 1, the same divisor, or 4, the results from 12 into 12 + 2 1 = 14; recirculation 1 >> 0 = 1, 1> = 0 does not meet the requirements
so the final quotient is 14.
You will not feel to think directly from the operational logic of the code, or that some do not understand?
Providers try to understand from the perspective of the dividend, divisor, remainder, quotient, the quotient may be any of a finite non-repeating 2 n- value is added to represent, for example above 14, can be expressed as 2 8 + 4 + 2 = 1 + 2 2 + 2 . 3 = 14; 100/3 another example, the provider 33, and the remainder is 1, in fact, can be expressed as supplier 32 = 2 1 + 1 + 2 . 5 = 33, if the statement is determined,(t >> i)> = d is in fact determines the dividend is not a divisor comprising a plurality of 2 IIf there is, the result accumulated 2 I , also need to subtract the dividend cook; if not, it is judged that there is no less 2 I , and so on, until a known cycle.

Guess you like

Origin blog.csdn.net/qq_41885819/article/details/102718981