Divide Two Integers leetcode
题目: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:
divisor = 0
;dividend = INT_MIN
anddivisor = -1
(becauseabs(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;
}
};
#include<iostream>
#include<limits>
using namespace std;
class Solution {
public:
int divide(int dividend, int divisor)
{
int sign = ((dividend > ) ^ (divisor > ) ? - : );
if (!divisor || (dividend==INT_MIN&&divisor==-))
return INT_MAX;
long long divid = labs(dividend), divis = labs(divisor);
long long res = ;
while (divid >= divis)
{
long long temp = divis,multi_time=;
while (divid >= (temp<<))
{
temp <<= ;
multi_time <<=;
}
divid -= temp;
res += multi_time;
}
return sign == ? res:-res;
}
};
int main()
{
Solution test;
int res = test.divide(, );
cout << res << endl;
return ;
}
Divide Two Integers leetcode的更多相关文章
- Divide Two Integers —— LeetCode
Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...
- Divide Two Integers leetcode java
题目: Divide two integers without using multiplication, division and mod operator. 题解: 这道题我自己没想出来...乘除 ...
- 29. Divide Two Integers - LeetCode
Question 29. Divide Two Integers Solution 题目大意:给定两个数字,求出它们的商,要求不能使用乘法.除法以及求余操作. 思路:说下用移位实现的方法 7/3=2, ...
- [LeetCode] Divide Two Integers 两数相除
Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...
- leetcode面试准备:Divide Two Integers
leetcode面试准备:Divide Two Integers 1 题目 Divide two integers without using multiplication, division and ...
- [Leetcode][Python]29: Divide Two Integers
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 29: Divide Two Integershttps://oj.leetc ...
- 【一天一道LeetCode】#29. Divide Two Integers
一天一道LeetCode系列 (一)题目 Divide two integers without using multiplication, division and mod operator. If ...
- LeetCode: Divide Two Integers 解题报告
Divide Two Integers Divide two integers without using multiplication, division and mod operator. SOL ...
- 【Leetcode】【Medium】Divide Two Integers
Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...
随机推荐
- 10.11 安装pod
原文地址:http://www.jianshu.com/p/5fc15906c53a 感谢. 更新升级10.11 cocoapods安装出问题最简单的解决方法 这是因为10.11把cocoapods ...
- 有了第一台自己开发的pro,开心,明天分享最近整理逆向分析ios的一些东西
最近都在忙于ios深入研究,研究别人的代码,别人的app.然后顺藤摸瓜的找到了关键:逆向,动态特性等. 相关工具:reveal, cycript等. 特别感谢前人的分享,为了打开学习ios的另外一扇大 ...
- python函数,lambda表达式,三目运算,列表解析,递归
一.自定义函数 定义函数时,函数体不执行:只有在调用函数时,函数体才执行.函数的结构: 1. def 2. 函数名 3. 函数体 def func_name(): 函数体 4. 返回值 如果没有声明返 ...
- 第五百八十一天 how can I 坚持
也是醉了,现在买个手机都特么搞饥饿营销,吹牛B就要付出吹牛B的代价,哎,好伤感. 晚上学习也没学好.感觉人和人之间的信任怎么都没了呢..但愿是我想多了,其实就是我想多了,以后说话还是要多注意. 睡觉吧 ...
- weed-fs 压力测试
阅读<Weed-FS/杂草文件系统 小文件存储集群 安装 使用 测试>中提到weedfs的负载压力不是很好,在看过代码后进行了相应测试,未发现负载压力有何问题. weedfs mast ...
- LIS检验系统,简介及主要特点
简介 主要实现实验室设备的联机管理和信息传输以及发布,其联机共享范围小到单机版,大到医院之间或区域互联,都可以任意选择,按需升级. 主要特点 打报告不用输入姓名,报告处理轻松.高效.无差错 检查项 ...
- sql按字段值进行统计
用group by 如有个student表里有性别sex来统计 select sex,count(*) from student group by sex;
- 自动化(Automation)兼容的数据类型
自动化(Automation)兼容的数据类型
- framebuffer应用编程实践
framebuffer的使用主要包括4个部分: (1):首先需要打开设备文件 /dev/fb0. (2):获取设备的信息.包括可变信息和不可变信息,分别使用两个结构体来进行封装,这两个结构体在 < ...
- C/ C++ 常见编程问题
C 中容易忽略的问题 1.在C语言中,浮点型变量分为两类: a. 单精度型:类型说明符为float, 在Turbo C 中占4个字节(32位)内存空间,其数值范围为3.4E-38~3.4E+38,可提 ...