29. Divide Two Integers
来源:互联网 发布:js获取url网页内容 编辑:程序博客网 时间:2024/05/21 09:33
Divide two integers without using multiplication, division and mod operator.
If it is overflow, return MAX_INT.
https://discuss.leetcode.com/topic/15568/detailed-explained-8ms-c-solution
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 we use? Yeah, bit manipulations.
Let’s do an example and see how bit manipulations work.
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 we can subtract the divisor
from the the dividend
without making the dividend
negative.
Let’s get started. We subtract 3
from 15
and we get 12
, which is positive. 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 result. Well, we shift again and get 12
. We subtract 12
from 15
and it 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 (initialized to be 0). 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 the full algorithm to perform division.
According to the problem statement, we need to handle some exceptions, such as overflow.
Well, two cases may cause overflow:
divisor = 0;dividend = INT_MIN and divisor = -1 (because abs(INT_MIN) = INT_MAX + 1).
Of course, we also need to take the sign into considerations, which is relatively easy.
Putting all these together, we have the following code.
class Solution {public: int divide(int dividend, int divisor) { if(divisor==0 || (dividend == INT_MIN && divisor == -1)) return INT_MAX; int sign = (dividend<0) ^ (divisor <0)? -1:1; long long dvd = labs(dividend); long long dvs = labs(divisor); int result = 0; while(dvd >= dvs){ long long temp = dvs,multi = 1; while(dvd >=(temp << 1)){ temp <<= 1; multi <<= 1; } dvd -= temp; result += multi; } return sign == 1?result:-result; }};
- 29. Divide Two Integers
- 29. Divide Two Integers
- 29. Divide Two Integers
- 29. Divide Two Integers
- 29. Divide Two Integers
- 29. Divide Two Integers
- 29. Divide Two Integers
- 29. Divide Two Integers
- 29. Divide Two Integers
- 29. Divide Two Integers
- 29. Divide Two Integers
- 29. Divide Two Integers
- 29. Divide Two Integers
- 29. Divide Two Integers
- 29. Divide Two Integers
- 29. Divide Two Integers
- 29. Divide Two Integers
- 29. Divide Two Integers
- 修改个人资料页面
- hdoj1301 Jungle Roads
- EA基础篇设置默认语言和默认数据库
- 渐变&马赛克——水晶格图标
- 【javase复习】## day11 其他流 ##
- 29. Divide Two Integers
- PyQt5教程(九)——绘图
- DNS域名解析
- 2016年8月7日,22岁生日
- 常量和变量
- linux 路由表设置 之 route 指令详解
- jboss: option requires an argument --c jboss启动报错
- 51nod oj 1256 1119 1013 1083 1014 1081<组合数+逆元+快速幂,DP,枚举,树状数组>
- Java控_01原创工具类(001)_深度的对象非null转换工具V1.0