leetcode笔记:Divide Two Integers

一. 题目描述

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

If it is overflow, return MAX_INT.

二. 题目分析

题目的意思简单明了,就是要求不使用乘法、除法和取余mod,输入两个整数,输出除法操作的结果。

出去乘除法,剩下的只有加减法和位运算,这是不难想到的,而直接使用减法,对被除数逐次减去除数大小的值,记录被减次数,肯定是可以得出和除法操作一样的结果,但该方法比较傻瓜,且会超时,时间复杂度为O(n)。

使用位运算可以做到O(logn)的复杂度,但要一步想到具体操作却也不那么简单,首先,我们知道任何一个整数都可以表示成以2的幂为底的一组基的线性组合,即num = flag0 * 2^0 + flag1 * 2^1 + flag2 * 2^2 + ... + flagn * 2^n 其中,flag0, flag1, flag2, ..., flagn 取值为0 & 1

基于以上事实,如果令:dividend / divisor = num,则有:

dividend = divisor * num = divisor * (flag0 * 2^0 + flag1 * 2^1 + flag2 * 2^2 + ... + flagn * 2^n)

对于除数,使用移位操作<<使其每次翻倍,从而减少减法求商的次数。以下是步骤:

  1. 当被除数大于除数时,对除数乘2(代码中使用变量step用于记录每次除数乘2),直到step大于被除数为止。记录移位操作的次数i。
  2. 如果被除数大于除数,那么被除数减去step。直到被除数小于除数。保存结果。
  3. 输出结果result。

注:

byte:128~127 (1Byte)
short :32768~32767 (2Bytes)
int:-2147483648~2147483647 (4Bytes)
long:-9223372036854774808~9223372036854774807 (8Bytes)

三. 示例代码

class Solution {
public:
    int divide(int dividend, int divisor) {
        if (dividend == 0 || divisor == 0) return 0;
        if (dividend == INT_MIN && divisor == -1) return INT_MAX; // 溢出
        bool negative = (dividend > 0 && divisor < 0) || (dividend < 0 && divisor > 0);
        long positiveDividend = abs(long(dividend));
        long positiveDivisor = abs(long(divisor));
        long result = 0;

        while (positiveDividend >= positiveDivisor) // 被除数大于除数
        {
            long step = positiveDivisor;
            for (int i = 0; positiveDividend >= step; ++i, step <<= 1)
            {
                positiveDividend = positiveDividend - step;
                result += 1 << i;
            }
        }
        return negative ? -result : result;
    }
};

四. 小结

使用位运算来解决此类问题,是挺容易想到的,但是具体如何操作又是另外一回事。

时间: 2024-08-11 07:37:39

leetcode笔记:Divide Two Integers的相关文章

LeetCode --- 29. Divide Two Integers

题目链接:Divide Two Integers Divide two integers without using multiplication, division and mod operator. If it is overflow, return MAX_INT. 这道题的要求是在不使用乘法.除法.取模运算的前提下实现两个整数相除.如果溢出,返回MAX_INT. 这道题的直接思路是用被除数不断减去除数,直到为0.这种方法的迭代次数是结果的大小,即比如结果为n,算法复杂度是O(n). 可以

LeetCode 28 Divide Two Integers

Divide two integers without using multiplication, division and mod operator. 思路:1.先将被除数和除数转化为long的非负数,注意一定要为long,因为Integer.MIN_VALUE的绝对值超出了Integer的范围. 2.常理:任何正整数num都可以表示为num=2^a+2^b+2^c+...+2^n,故可以采用2^a+2^b+2^c+...+2^n来表示商,即dividend=divisor*(2^a+2^b+

[LeetCode] 029. Divide Two Integers (Medium) (C++/Python)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 029. Divide Two Integers (Medium) 链接: 题目:https://oj.leetcode.com/problems/divide-two-integers/ 代码(github):https://github.com/illuz/leetcode 题意: 实现除法,不能用乘.除和取模

leetcode之Divide Two Integers

Divide Two Integers Divide two integers without using multiplication, division and mod operator. 分析:不用乘.除.模运算实现除法.由于每个整数都可以写成an*2^n + --+a0*2^0的形式,所以可以使用移位来求商,首先是递归算法,此算法在leetcode上超时,不过理解起来比较简单,思路也是正确的. class Solution { public: int __div(int x,int y)

【leetcode】Divide Two Integers (middle)☆

Divide two integers without using multiplication, division and mod operator. If it is overflow, return MAX_INT. 思路: 尼玛,各种通不过,开始用纯减法,超时了. 然后用递归,溢出了. 再然后终于开窍了,用循环,把被除数每次加倍去找答案,结果一遇到 -2147483648 就各种不行, 主要是这个数一求绝对值就溢出了. 再然后,受不了了,看答案. 发现,大家都用long long来解决溢

【leetcode】Divide Two Integers

题目: Divide two integers without using multiplication, division and mod operator. 解析:不使用乘号.除号和余号实现两个整数的除法.该题可以利用移位操作数求解,主要分为三个步骤: (1)先求两个int类型整数的绝对值,注意要将int类型转化成long类型才能求绝对值:因为int类型的范围是-2147483648~2147483647,如果某个数为-2147483648,其绝对值会溢出. (2)对被除数a和除数b进行移位

LeetCode 29 Divide Two Integers(两个整数相除)(*)

翻译 不用乘法.除法.取余操作,将两个数相除. 如果它溢出了,返回MAX_INT 原文 Divide two integers without using multiplication, division and mod operator. If it is overflow, return MAX_INT. 代码 一心扑到了递归上,可惜没能写出来----烦躁至极还是找了别人的答案-- class Solution { public: int divide(int dividend, int d

[LeetCode]74. Divide Two Integers除法运算

Divide two integers without using multiplication, division and mod operator. If it is overflow, return MAX_INT. Subscribe to see which companies asked this question 解法1:因为题目限制不能用乘法.除法和取模操作,考虑到除法可以使用减法来做,因此可以让被除数不断减去除数,直到小于或者等于0,而循环累计次数就是结果.这样做效率不高,在被

leetCode 29.Divide Two Integers (两整数相除) 解题思路和方法

Divide Two Integers Divide two integers without using multiplication, division and mod operator. If it is overflow, return MAX_INT. 思路:这个题算法上不是很难,但是通过率相当低,只有15%,果然,自己在写完之后,各种出错,而且错误不是算法上的错误,是各种边界值没有考虑到,很多溢出错误等.下面是具体代码,有详细注释. public class Solution { p

LeetCode 029 Divide Two Integers

题目要求:Divide Two Integers Divide two integers without using multiplication, division and mod operator. If it is overflow, return MAX_INT. 分析: 不能用乘.除和取余,则只能用减了…… 代码如下: class Solution { public: int divide(int dividend, int divisor) { // 当 dividend = INT