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 ...
随机推荐
- 华为RH2285安装过程及经验总结
安装测试 1.由于服务器为二手服务器,噪音相对较大,如果未经改造无法正常使用,当机器第一次运行的时候,我的血压一下升高不少. 第一步 服务器的远程端口和路由器连接在一起 第二步 设置服务器的bio ...
- hive 全局排序
不分发数据,使用单个reducer ; select * from dw.dw_app where dt>='2016-09-01' and dt <='2016-09-18' order ...
- nodejs http 请求延时的处理方法(防止程序崩溃)
有时候因为接口没开,或者其他原因,导致http.request 请求延时,一直耗费资源不说,还会导致程序报错崩溃,延时处理其实也是一种错误处理. 直接上代码: var APIGET = functio ...
- PIC32MZ tutorial -- 32-bit Timer
The microcontroller is PIC32MZ2048ECH144 on the PIC32MZ EC Starter Kit. This microcontroller has fou ...
- NHibernate系列文章三:简单的增删改查询
摘要 上一篇文章只完成了简单的NHibernate安装.配置和连接数据库.这篇文章介绍怎样实现最简单的数据库读写操作. 1. 重构ISessionFactory生成过程 将生成ISessionFact ...
- 相同的问题又出现了,struts2取不出数值
debug里面是有数值的,不知道是不是又是表示错了.全部改成了小写也无济于事.正在想法解决中... 问题解决了,因为自己的不仔细,问题还是出在了action的set,get方法里,不是大小写没注意,改 ...
- 第五百八十六天至第六百零五天 how ccan I 坚持
考研中,勿扰... 我是个逗比,哈哈. 时间不够用了呢,哎.
- Python自动化 【第一篇】:Python简介和入门
Python简介: 一.什么是python Python是一门动态解释性的强类型定义语言. pythonde 特点:“优雅”.“明确”.“简单”. 二.Python由来 python的创始人为吉多·范 ...
- 优雅的设计单线程范围内的数据共享(ThreadLocal)
单线程范围内数据共享使用ThreadLocal /** * @Description TODO * @author zhanghw@chinatelecom.cn * @since 2015年12月1 ...
- ecshop的弊病和需要修改的地方,持续更新
ecshop的session机制是基于cookie的,用数据库进行缓存,当浏览器关闭cookie,sessions表会爆表,所以需要改进. 在cls_template.php中 $_echash值是固 ...