[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 > right)
            return -1;

        int mid = left + (right-left)/2;
        if(a[mid]==key)
            return mid;
        else if(a[mid]<key){
           int pos = bsearch(a,mid+1,right,key);
           return pos == -1 ?mid :pos;
        }else
            return bsearch(a,left,mid-1,key);
    }

    int divide(int devidend,int divisor){
        int sign = devidend <0 ?-1:1;
        if(divisor<0)
            sign *= -1;

        long long div = devidend;
        div = abs(div);
        long long divisorL = divisor;
        divisorL = abs(divisorL);
        f.push_back(divisorL);
        int size = 1;

        while(true){
            if(f[size-1] >= div)
                break;
            f.push_back(f[size-1]+f[size-1]);
            size++;
        }//end while

        int num = 0;
        long long sum = 0;
        while(div>0){
           int pos = bsearch(f,0,size-1,div);
           if(pos == -1)
               break;
           div -= f[pos];
           num += (1<< pos);
        }//end while
       return num*sign;
    }
};
时间: 2024-10-25 21:17:20

[LeetCode] Divide Two Integers( bit + 二分法 )的相关文章

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&mdash;&mdash;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 divi

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+