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 ...
随机推荐
- Qlikview 的权限控制
Qlikview报表控件/数据的权限控制,首先在“文档属性”->“打开”-> 勾选“基于访问权限的初始数据减少”, 这样打开报表的时候会提示输入用户名和密码. Qlikview 的权限控制 ...
- regular expressions
- memory allocation
1 malloc与free是C++/C语言的标准库函数,new/delete是C++的运算符. 2,malloc, 必须 包含头文件<stdlib.h> 3, nt* p_scalar = ...
- JQuery mobile 实例 api
http://www.w3school.com.cn/jquerymobile/jquerymobile_examples.asp
- 2015年毕业生收到的offer和薪资透露
个人情况:全国排名70多的大学,非211,计算机专业硕士 收到的offer: 苏州同程旅游--6k/月 ,自己要了这么多,就给了这么多,没有规范.苏州蜗牛科技--8k/月 ,给了个口头offer,要去 ...
- Docx读写Word
Docx.dll功能比较强大,具备以下功能: 创建新的word文档或者读取已有的world文档 替换书签处内容: 插入表格或者在已有表格新增数据行: 插入图片,轻松设置图片大小: 保存或者另存为: 分 ...
- error in opening zip file 1 错误
项目部署服务启动时会出现: error in opening zip file 1 错误 原来是不同服务器编译过的jar包直接下载后发布有问题,重新上传本地编译好的lib下面的jar包后,启动服务,正 ...
- HTML5基础总结
HTML5是HTML语言的第五次重大版本升级,新增了如下内容:1.新增<video>.<audio>标签在页面上直接播放多媒体资源;2.新增<input>标签的ty ...
- Android-Service生命周期
Service的基本概念,以及Service的生命周期: 一.Service的基本概念: 一个Service就是应用程序的组件,可以在后台长期跑,或者是为其他的应用提供功能上的支持.Service一般 ...
- An unknown error occurred & “”的 iPhone is busy: Processing symbol files
An unknown error occurred & ""的 iPhone is busy: Processing symbol files An unknown err ...