[LeetCode] 29. Divide Two Integers

</br>


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

If it is overflow, return MAX_INT.


</br>

Solution

The goal is to perform division without using multiplication, division or mod operator.

The most straightforward way to subtract dividend with divisor n times to return the divide is clearly not gonna work and will results in TLE.

We have to use bit manipulations.

Suppose we want to divide 15 by 3, so 15 is dividend and 3 is divisor.

[1] We subtract 3 from 15 and we get 12, which is positive.
[2] Shift 3 to the left by 1 bit and we get 6. Subtracting 6 from 15 still gives a positive result.
[3] Shift again and we get 12. We subtract 12 from 15 and it is still positive.
[4] Shift again, obtaining 24 and we know we can at most subtract 12.

Since 12 is obtained by shifting 3 to left twice, we know it is 4 times of 3. How do we obtain this 4? Well, we start from 1 and shift it to left twice at the same time.

And we implement this algorithm by using recursion to make code more efficient and makes more sense.

What is more, we have to consider the overflow situation. When the input contains Integer.MAX/MIN_VALUE, the algorithm should be able to handle this number. To achieve which, we convert numbers into long instead of int, and in this way, not only can we solve the problem when input is around Integer.MAX_VALUE, we have also deal with the situation when the input is

2147483648
2

and the divisor shift to the left may be overflow, by converting everything into long, we can avoid this problem.

The code is shown as below.

Java

public class Solution {
    public int divide(int dividend, int divisor) {
        
        //Special Situation & Deal With Sign
        if (divisor == 0 || (dividend == Integer.MIN_VALUE && divisor == -1))
            return Integer.MAX_VALUE;
        int sign = ((dividend<0 && divisor>0) || (dividend>0 && divisor<0)) ? -1 : 1;
        
        //Initialization
        long divide = 0;
        long divisor_abs = Math.abs((long)divisor),dividend_abs = Math.abs((long)dividend);
        
        divide = subdivide(dividend_abs,divisor_abs);
        return sign == 1 ? (int)divide:(int)(-divide);
    }
    
    //Recursion
    public long subdivide(long dividend_abs, long divisor_abs){
        if(dividend_abs < divisor_abs)
            return 0;
                
        int times = 1,divide = 0;
        long divisor_temp = divisor_abs;
                
        while(dividend_abs >= (divisor_temp<<1)){
            divisor_temp <<= 1;
            times <<= 1;
        }
        dividend_abs -= divisor_temp;
        divide += times;
        
        return divide + subdivide(dividend_abs,divisor_abs);
    }
}

</br>

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 高德API介绍的还算不错,只是有些细节的地方还需要注意一下,说一下我遇到的问题: 自定义的大头针点击没有响应(调试...
    钎探穗阅读 334评论 0 1
  • 看到一句话:现在对他有深深的防备、他已不是那个能依靠的人、只能共苦不能同甘的人、很少吵架了因为不值得、省口气暖暖心...
    泥浆阅读 183评论 0 0
  • 你问我孤独是什么。 一个人吃饭。 一个人散步。 一个人去影院看电影。 这些都不算。 宿舍的风扇开到第三档,呼啦呼啦...
    黃昏晨曉_念雁落蝶飛阅读 999评论 0 0
  • 渐渐忙起来了,是我期待的,也是我担心的。对于我的生活来说,不确定是占多数的,而可以确定的我是个闲不住的人。 闲不住...
    博峰庸者阅读 673评论 2 49