博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Divide Two Integers leetcode
阅读量:6307 次
发布时间:2019-06-22

本文共 3282 字,大约阅读时间需要 10 分钟。

 题目:Divide Two Integers

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

If it is overflow, return MAX_INT.

看讨论区大神的思路:

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 be0). 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:

  1. divisor = 0;
  2. 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 || (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 res = 0; while (dvd >= dvs) { long long temp = dvs, multiple = 1; while (dvd >= (temp << 1)) { temp <<= 1; multiple <<= 1; } dvd -= temp; res += multiple; } return sign == 1 ? res : -res; } };
1 #include
2 #include
3 using namespace std; 5 class Solution { 6 public: 7 int divide(int dividend, int divisor) 8 { 9 int sign = ((dividend > 0) ^ (divisor > 0) ? -1 : 1);10 if (!divisor || (dividend==INT_MIN&&divisor==-1))11 return INT_MAX;12 long long divid = labs(dividend), divis = labs(divisor);13 long long res = 0;14 while (divid >= divis)15 {16 long long temp = divis,multi_time=1;17 while (divid >= (temp<<1))18 {19 temp <<= 1;20 multi_time <<=1;21 }22 divid -= temp;23 res += multi_time;24 }25 return sign == 1 ? res:-res;26 }27 };28 int main()29 {30 Solution test;31 int res = test.divide(0, 1);32 cout << res << endl;33 return 0;34 }

 

转载于:https://www.cnblogs.com/chess/p/5065378.html

你可能感兴趣的文章
初识闭包
查看>>
java tcp socket实例
查看>>
011 指针的算术运算
查看>>
hdu1874畅通工程续
查看>>
rails 字符串 转化为 html
查看>>
java-学习8
查看>>
AOP动态代理
查看>>
Oracle序列
查看>>
xcodebuild命令行编译错误问题解决
查看>>
Yii2.0 下的 load() 方法的使用
查看>>
华为畅玩5 (CUN-AL00) 刷入第三方twrp Recovery 及 root
查看>>
LeetCode----67. Add Binary(java)
查看>>
母版页 MasterPage
查看>>
[转] ReactNative Animated动画详解
查看>>
DNS原理及其解析过程
查看>>
记录自写AFNetWorking封装类
查看>>
没想到cnblog也有月经贴,其实C#值不值钱不重要。
查看>>
【转】LUA内存分析
查看>>
springboot使用schedule定时任务
查看>>
[转] Entity Framework Query Samples for PostgreSQL
查看>>