Likou brushing notes: 29. Divide two numbers (multiplication method, easy-to-understand code, does not use any multiplication and division operations)

topic:

29, divide two numbers

Given two integers, dividend and divisor. Divide two numbers without using multiplication, division and mod operators.

Returns the quotient obtained by dividing the dividend by the divisor.

The result of integer division should be truncated (truncate) the decimal part, for example: truncate(8.345) = 8 and truncate(-2.7335) = -2

Example 1:

Input: dividend = 10, divisor = 3
Output: 3
Explanation: 10/3 = truncate(3.33333…) = truncate(3) = 3

Example 2:

Input: dividend = 7, divisor = -3
Output: -2
Explanation: 7/-3 = truncate(-2.33333…) = -2

prompt:

Both the dividend and the divisor are 32-bit signed integers.
The divisor is not zero.
Assuming that our environment can only store 32-bit signed integers, the value range is [−231, 231 − 1]. In this question, if the division result overflows, 231 − 1 is returned.

Problem solution ideas:

The simplest idea is to make both numbers positive, then the dividend is continuously subtracted by the number until it is less than the divisor, and finally the plus or minus sign is added. You can use XOR to determine whether the positive and negative of two numbers are the same, and the only possibility of overflow is that the result is equal to 2^31. Of course, this method will time out in the use case (-2147483648, -1). . .

No timeout ideas:

Obviously the timeout is because the approximation of two numbers is too slow. Because division cannot be used, subtraction must be used only to change the dividend, which is too slow.

So consider changing the divisor, you can continue to add, which is equivalent to multiplying by 2, and you can quickly approximate the dividend.

For example (100, 2), 2 is continuously added up to 64 and less than 100, then 100 should be subtracted from 64 to get the fastest approximation, and subtracting 64 is equivalent to subtracting 32 2s.

But because division cannot be used, 32 is not obviously obtained, so a variable is required to be initially 1, which is added as the divisor is added to represent the equivalent number of divisors.

After subtracting 64 from 100, the dividend and divisor become (36, 2), and then the process can be repeated.

Further, there are obviously a lot of repeated calculations later, so you can save the values ​​4, 8, 16, 32, 64 and the equivalent divisors in the process, and then traverse in reverse order.

Problem solution python code:

class Solution:
    def divide(self, dividend: int, divisor: int) -> int:
        m, n, flag = abs(dividend), abs(divisor), int(dividend^divisor >=0)
        d, w = [], 1
        while n <= m:
            d.append((n, w))
            n += n
            w += w
        res = 0
        for n, w in d[::-1]:
            if m >= n:
                m -= n
                res += w
        return min([-res, res][flag], 2147483647)

Insert picture description here

Author: huozhixue
link: https://leetcode-cn.com/problems/divide-two-integers/solution/python3-jian-ji-by-huozhixue-xgo6/
Source: stay button (LeetCode) HTTPS: // leetcode- cn.com/problems/divide-two-integers/

Guess you like

Origin blog.csdn.net/weixin_44414948/article/details/113797624