Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[LeetCode] 29. Divide Two Integers #29

Open
grandyang opened this issue May 30, 2019 · 2 comments
Open

[LeetCode] 29. Divide Two Integers #29

grandyang opened this issue May 30, 2019 · 2 comments

Comments

@grandyang
Copy link
Owner

@grandyang grandyang commented May 30, 2019

 

Given two integers dividend and divisor, divide two integers without using multiplication, division and mod operator.

Return the quotient after dividing dividend by divisor.

The integer division should truncate toward zero.

Example 1:

Input: dividend = 10, divisor = 3
Output: 3

Example 2:

Input: dividend = 7, divisor = -3
Output: -2

Note:

  • Both dividend and divisor will be 32-bit signed integers.
  • The divisor will never be 0.
  • Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231,  231 − 1]. For the purpose of this problem, assume that your function returns 231 − 1 when the division result overflows.

 

这道题让我们求两数相除,而且规定不能用乘法,除法和取余操作,那么这里可以用另一神器位操作 Bit Manipulation,思路是,如果被除数大于或等于除数,则进行如下循环,定义变量t等于除数,定义计数p,当t的两倍小于等于被除数时,进行如下循环,t扩大一倍,p扩大一倍,然后更新 res 和m。这道题的 OJ 给的一些 test case 非常的讨厌,因为输入的都是 int 型,比如被除数是 -2147483648,在 int 范围内,当除数是  -1 时,结果就超出了 int 范围,需要返回 INT_MAX,所以对于这种情况就在开始用 if 判定,将其和除数为0的情况放一起判定,返回 INT_MAX。然后还要根据被除数和除数的正负来确定返回值的正负,这里采用长整型 long 来完成所有的计算,最后返回值乘以符号即可,代码如下:

 

解法一:

class Solution {
public:
    int divide(int dividend, int divisor) {
        if (dividend == INT_MIN && divisor == -1) return INT_MAX;
        long m = labs(dividend), n = labs(divisor), res = 0;
        int sign = ((dividend < 0) ^ (divisor < 0)) ? -1 : 1;
        if (n == 1) return sign == 1 ? m : -m;
        while (m >= n) {
            long t = n, p = 1;
            while (m >= (t << 1)) {
                t <<= 1;
                p <<= 1;
            }
            res += p;
            m -= t;
        }
        return sign == 1 ? res : -res;
    }
};

 

我们可以通过递归的方法来解使上面的解法变得更加简洁:

 

解法二:

class Solution {
public:
    int divide(int dividend, int divisor) {
        long m = labs(dividend), n = labs(divisor), res = 0;
        if (m < n) return 0;
        long t = n, p = 1;
        while (m > (t << 1)) {
            t <<= 1;
            p <<= 1;
        }
        res += p + divide(m - t, n);
        if ((dividend < 0) ^ (divisor < 0)) res = -res;
        return res > INT_MAX ? INT_MAX : res;
    }
};

 

Github 同步地址:

#29

 

参考资料:

https://leetcode.com/problems/divide-two-integers/

https://leetcode.com/problems/divide-two-integers/discuss/13524/summary-of-3-c-solutions

https://leetcode.com/problems/divide-two-integers/discuss/13407/C%2B%2B-bit-manipulations

https://leetcode.com/problems/divide-two-integers/discuss/142849/C%2B%2BJavaPython-Should-Not-Use-%22long%22-Int

 

LeetCode All in One 题目讲解汇总(持续更新中...)

@baicaihenxiao
Copy link

@baicaihenxiao baicaihenxiao commented Apr 29, 2020

Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range.

应该是不能用long定义变量的

@jiangyewen
Copy link

@jiangyewen jiangyewen commented Oct 20, 2020

Really hate this Problem. SO many overflow inputs... After hours of commits, finally fixed the overflow inputs...

class Solution {
public:
    int divide(int dividend, int divisor) {
        if (dividend == INT_MIN && divisor == -1) return INT_MAX;
        if (dividend == INT_MIN && divisor == INT_MIN) return 1;
        if (dividend == INT_MIN && divisor == INT_MAX) return -1;
        if (divisor == INT_MIN) return 0;
        if (divisor == 0) return INT_MAX;
        if (divisor == 1) return dividend;

        int result = 0;
        int count = 0;
         if(dividend == INT_MIN){
             dividend += abs(divisor);
             count ++;
             result += count;
         }
        int sign = ((dividend > 0 && divisor > 0)||(dividend < 0 && divisor < 0) )? 1 : -1;
        int curDividend = abs(dividend);
        int curDivisor = abs(divisor);
        while (curDividend > 0) {
            if(curDivisor <= INT_MAX /2 && ((curDivisor << 1) <= curDividend )){
                //multiply divisor by 2 to be faster
                curDivisor <<= 1;
                curDividend -= curDivisor;
                if(count >0) {
                    //also multiply count by 2
                    count <<= 1;
                } else{
                    count = 2;
                }
                result += count;
            } else {
                curDividend -= abs(divisor);
                if (curDividend >= 0) {
                    result ++;
                }
            }
            
        }
        return sign>0? result : -result;
    }
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants