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

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

题目只有简单的一句话,看起来可真简单啊,呵呵,假象。这个题目的难点在于对时间效率的限制和边界值的测试。第一印象肯定是循环一个个把因子从被除数中减去不久行了么,可是对于比如INT_MAX/1或者INT_MIN/1之类的执行时间长的可怕,会超出时间限制。改善时间效率的思路是参考网上别人代码,将因子不断乘以2(可以通过移位实现,同时结果也从1开始不断移位加倍),然后和被除数比较,等到大于被除数一半了,就从被除数中减去,将因子个数叠加入结果中。然后在剩下的被除数中采用同样的方法减去小于其一半的因子和,循环往复。我在代码中使用了unsigned
int类型存储因子和,于是存在这种情况INT_MIN/INT_MIN,这样如果在循环条件判定是加上等于的情况,因子和移位时可能发生越界,所以等于情况单独考虑了。code如下:

class Solution {
public:
    int divide(int dividend, int divisor) {
        assert(divisor != 0);
        int count = 0, result = 0;
        bool isNeg = false;
        if((dividend<0 && divisor>0)||(dividend>0 && divisor<0))
            isNeg = true;
        unsigned int tDividend = abs(dividend);
        unsigned int tDivisor = abs(divisor);
        unsigned int sum = 0;
        while(tDivisor < tDividend)
        {
            count = 1;
            sum = tDivisor;
            while((sum<<=1) < tDividend)
            {
                count<<=1;
            }
            tDividend -= (sum>>=1);
			result += count;
        }
        if(tDivisor == tDividend)
            result++;
        return isNeg ? (0-result) : result;
    }
};

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

时间: 2024-08-05 18:33:05

leetcode——Divide Two Integers 不用乘除取余操作求除法(AC)的相关文章

LeetCode: Divide Two Integers [028]

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

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 解题报告

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

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( 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

leecode---07---数字整除取余操作,取余整除---翻转一个整数复件

https://leetcode.com/problems/reverse-integer/description/ 题意 将一个整数进行翻转. Example 1: Input: 123 Output: 321 Example 2: Input: -123 Output: -321 Example 3: Input: 120 Output: 21 分析 给一个整数,将这个整数进行翻转,从后往前每一位先取余出来. 最后要判断是否越界处理. 代码 class Solution { public i

Java之取余操作 &quot;%&quot;

取模运算与取余运算两个概念有重叠的部分但又不完全一致.主要的区别在于对负整数进行除法运算时操作不同. 对于整形数a,b来说,取模运算或者求余运算的方法都是: 1.求 整数商 c = a / b: 2.计算模或者余数 r = a - c* b . 取模运算和取余运算在第一步不同: 取余运算在取c值时,向0方向舍入:而取模运算在取c值时,是向负无穷方向舍入 各个环境下运算符%的含义不同,C/C++,Java为取余,python为取模 Java取余运算规则如下: a%b = a - (a/b)*b 原

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

kb-01-e&lt;取余操作,宽搜,巧妙&gt;;

题目描述: n属于1到200,找到对应的一个数只含有0和1,并且是n的倍数: 分析: 本题有几个数会是大数:所以要考虑大数: 用到余数的性质:例如n=6,1%6=1: 1*10%6=4:              (1*10+1)%6=5: 4*10%6=4:               (4*10+1)%6=5: 5*10%6=2:                (5*10+1)%6=3: (重复4,5) 2*10%6=2:                  ....=3: 3*10%6=0: