资讯专栏INFORMATION COLUMN

[LeetCode] 29. Divide Two Integers

fai1017 / 1920人阅读

Problem

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: [−2^31, 2^31 − 1]. For the purpose of this problem, assume that your function returns 2^31 − 1 when the division result overflows.

Solution
class Solution {
    public int divide(int dividend, int divisor) {
        int sign = 1;
        if (dividend > 0 && divisor < 0 || (dividend < 0 && divisor > 0)) sign = -1;
        long dd = Math.abs((long) dividend);
        long ds = Math.abs((long) divisor);
        
        if (ds == 0) return Integer.MAX_VALUE;
        if (dd == 0 || dd < ds) return 0;
        
        long quotient = divideHelper(dd, ds);
        int res;
        if (quotient > Integer.MAX_VALUE) {
            res = sign == 1 ? Integer.MAX_VALUE : Integer.MIN_VALUE;
        } else {
            res = (int) (sign * quotient);
        }
        return res;
    }
    private long divideHelper(long dd, long ds) {
        if (dd < ds) return 0;
        long sum = ds;
        long count = 1;
        while (sum <= dd - sum) {
            sum += sum;
            count += count;
        }
        return count + divideHelper(dd-sum, ds);
    }
}

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/71866.html

相关文章

  • leetcode-29. Divide Two Integers

    摘要:题目解析用加减法实现除法用减法,每次累加被减部分,累加商,以一个固定的倍数递增坑注意循环的跳出便捷,的情况要注意。应用累加思想,可以用在提速上,效率提高如果,则是负的,则是正的 题目解析: 用加减法实现除法 用减法,每次累加被减部分,累加商, 以一个固定的倍数递增 坑: 注意 while循环的跳出便捷,= 的情况要注意。应用:累加思想,可以用在提速上,效率提高 Given two ...

    darkbaby123 评论0 收藏0
  • leetcode29 Divide Two Integers

    摘要:题目要求在不使用乘法,除法和求余操作的情况下,计算两个整数相除的结果。如果溢出了,则返回最大值。在这里核心思路是使用逆向二分法和递归的思路来进行计算。在这里我们使用取值范围更广的来处理数值溢出的场景。 题目要求 Divide two integers without using multiplication, division and mod operator. If it is o...

    cnio 评论0 收藏0
  • leetcode 29 Divide Two Integers

    摘要:很容易想到,我们每次用被除数减去除数,进行减法的次数就是最终结果。这道题的采取了一种类似二分查找的思想。除了这些,这道题还要注意一些边界情况的判断,例如除数或被除数为,值溢出等。 题目详情 Divide two integers without using multiplication, division and mod operator.If it is overflow, retu...

    马龙驹 评论0 收藏0
  • LeetCode刷题——29. Divide Two Integers(Part 2靠大家)

    摘要:上篇文章写了以我自己的思路来解决这个问题,但是运行时间过长,看了上的高效写法是使用位运算的解法,当初我自己写这个问题是也想到了可以用位运算快一点,但是因为基础差,对位运算的掌握不牢靠,还是选择使用了减法的思路,在此将上高效解法做一个思路分析 上篇文章写了以我自己的思路来解决这个问题,但是运行时间过长,看了leetcode 上的高效写法是使用位运算的解法,当初我自己写这个问题是也想到了可...

    JouyPub 评论0 收藏0
  • [Leetcode] Divide Two Integers 整数整除

    摘要:位操作法复杂度时间空间思路我们设想,本来应该的得到余,那么如果我们把忽略余数后分解一下,,也就是,也就是把商分解为,所以商的二进制是。我们可以不断的将乘的一次方,二次方,等等,直到找到最大那个次方,在这里是的四次方。 Divide Two Integers Divide two integers without using multiplication, division and m...

    张春雷 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<