leetcode——Divide Two Integers

题目:

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

If it is overflow, return MAX_INT.

题意:

不用乘号、除号、取模运算来模拟除法。

分析:

一开始每回减去一次除数,这样会超时,优化是减去除数*2^n,用左移运算可以实现每次翻2倍.

class Solution {
public:
    // 每次被除数翻倍,来增加效率
    int divide(int dividend, int divisor) {
        // c左移n位就是c*2^n
        // 先转为正数再运算,因为INI_MIN转为正数会溢出,所以用先转为longlong类型再转为负数
        long long a = dividend>=0 ? dividend : -(long long)dividend;
        long long b = divisor>=0 ? divisor : -(long long)divisor;
        // 被除数每次翻倍,若超过除数了再从1倍开始翻倍
        // i是倍数
        long long ret  = 0;
        while(a>=b){
            long long c = b;
            for(int i = 0; a >= c; i ++, c <<= 1){
                a -= c;
                ret += (1<<i);
            }
        }
        // 当被除数是-2147483648时,异或可能会溢出,所以异或也要用longlong类型
        // 异或除数和被除数,若都是同号则为假,异号则为真
        // 因为最左边第64位是符号位,所以要右移63位
        ret = (((long long)(dividend^divisor))>>63) ? -ret : ret;
        if(ret>INT_MAX) return INT_MAX;
        return ret;
    }
};
时间: 2024-11-06 13:31:24

leetcode——Divide Two Integers的相关文章

LeetCode: Divide Two Integers [028]

[题目] Divide two integers without using multiplication, division and mod operator. [题意] 计算两个数的商,不能使用乘.除.取余操作 [思路] 用加法,确定多少除数相加其和恰好<=被除数 为了提高算法效率,利用贪心思想,采用滚雪球式的翻倍叠加策略,使和快速逼近被除数 几种特殊情况需要注意: 1. 结果是负数 2. 除数的绝对值要比被除数的绝对值大 3. 除数是0 4. 被除是0 5. 注意除数翻倍累加时越界,超过i

leetcode——Divide Two Integers 不用乘除取余操作求除法(AC)

Divide two integers without using multiplication, division and mod operator. 题目只有简单的一句话,看起来可真简单啊,呵呵,假象.这个题目的难点在于对时间效率的限制和边界值的测试.第一印象肯定是循环一个个把因子从被除数中减去不久行了么,可是对于比如INT_MAX/1或者INT_MIN/1之类的执行时间长的可怕,会超出时间限制.改善时间效率的思路是参考网上别人代码,将因子不断乘以2(可以通过移位实现,同时结果也从1开始不断

[LeetCode] Divide Two Integers

In this problem, we are asked to divide two integers. However, we are not allowed to use division, multiplication and mod operations. So, what else can be use? Well, bit manipulations. Let's look at an example and see how bit manipulation might help.

LeetCode: Divide Two Integers 解题报告

Divide Two Integers Divide two integers without using multiplication, division and mod operator. SOLUTION 11. 基本思想是不断地减掉除数,直到为0为止.但是这样会太慢. 2. 我们可以使用2分法来加速这个过程.不断对除数*2,直到它比被除数还大为止.加倍的同时,也记录下cnt,将被除数减掉加倍后的值,并且结果+cnt. 因为是2倍地加大,所以速度会很快,指数级的速度. 3. 另外要注意的是

[LeetCode] Divide Two Integers( bit + 二分法 )

Divide two integers without using multiplication, division and mod operator. 常常出现大的负数,无法用abs()转换成正数的情况 class Solution{ private: vector<long long> f; public: int bsearch(vector<long long> &a,int left,int right,long long key){ if(left > r

leetcode Divide Two Integers python

class Solution(object): def divide(self, dividend, divisor): """ :type dividend: int :type divisor: int :rtype: int """ flag=-1 if ( dividend > 0 and divisor >0 ) or ( dividend < 0 and divisor < 0 ): flag=1 divide

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][Python]29: Divide Two Integers

# -*- coding: utf8 -*-'''__author__ = '[email protected]' 29: Divide Two Integershttps://oj.leetcode.com/problems/divide-two-integers/ Divide two integers without using multiplication, division and mod operator.If it is overflow, return MAX_INT. ===C

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+