[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.

Suppose we want to divide 15 by 3, so 15 is dividend and 3 is divisor. Well, division simply requires us to find how many times can we subtract the divisor from the the dividend without making the latter negative. Let‘s get started. We subtract 3 from 15 and we get 12, which is positive, so let‘s try to subtract more. Well, we shift 3 to the left by 1 bit and we get 6. Subtracting 6 from 15 still gives a positive answer. Well, we shift again and get 12. We subtract 12 from 15 and the answer is still positive. We shift again, obtaining 24 and we know we can at most subtract 12. Well, since 12 is obtained by shifting 3 to left twice, we know it is 4 times of 3. How do we obtain this 4? Well, we start from 1 and shift it to left twice, at the same time. We add 4 to an answer. In fact, the above process is like 15 = 3 * 4 + 3. We now get part of the quotient (4), with a remainder 3.

Then we repeat the above process again. We subtract divisor 3 from the remaining dividend 3 and obtain 0. We know we are done. No shift happens, so we simply add 1 << 0 to the answer.

Now we have had the full algorithm to perform division.

According to the problem statement, we need to handle some exceptions, such as flow.

Well, two cases may cause overflow:

  1. divisor = 0;
  2. dividend = INT_MIN and divisor = -1 (because abs(INT_MIN) = abs(INT_MAX) + 1).

Of course, we also need to take the sign into account, which is relatively easy.

Putting things together, we have the following code.

 1     int divide(int dividend, int divisor) {
 2         if (!divisor || (dividend == INT_MIN && divisor == -1))
 3             return INT_MAX;
 4         long long dvd = labs(dividend);
 5         long long dvs = labs(divisor);
 6         int sign = (dividend > 0) ^ (divisor > 0) ? -1 : 1;
 7         int res = 0;
 8         while (dvd >= dvs) {
 9             long long tmp = dvs, multiple = 1;
10             while (dvd >= (tmp << 1)) {
11                 tmp <<= 1;
12                 multiple <<= 1;
13             }
14             dvd -= tmp;
15             res += multiple;
16         }
17         return sign * res;
18     }
时间: 2024-09-30 00:51:27

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

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&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+