Given two integers `dividend` and `divisor`, divide two integers without using multiplication, division and mod operator.

Return the quotient after dividing `dividend` by `divisor`.

The integer division should truncate toward zero.

Example 1:

```Input: dividend = 10, divisor = 3
Output: 3```

Example 2:

```Input: dividend = 7, divisor = -3
Output: -2```

Note:

• Both dividend and divisor will be 32-bit signed integers.
• The divisor will never be 0.
• Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231,  231 − 1]. For the purpose of this problem, assume that your function returns 231 − 1 when the division result overflows.

```class Solution {
public:
int divide(int dividend, int divisor) {
if (dividend == INT_MIN && divisor == -) return INT_MAX;
long m = labs(dividend), n = labs(divisor), res = ;
int sign = ((dividend < ) ^ (divisor < )) ? - : ;
if (n == ) return sign ==  ? m : -m;
while (m >= n) {
long t = n, p = ;
while (m >= (t << )) {
t <<= ;
p <<= ;
}
res += p;
m -= t;
}
return sign ==  ? res : -res;
}
};```

```class Solution {
public:
int divide(int dividend, int divisor) {
long m = labs(dividend), n = labs(divisor), res = ;
if (m < n) return ;
long t = n, p = ;
while (m > (t << )) {
t <<= ;
p <<= ;
}
res += p + divide(m - t, n);
if ((dividend < ) ^ (divisor < )) res = -res;
return res > INT_MAX ? INT_MAX : res;
}
};```

Github 同步地址：

https://github.com/grandyang/leetcode/issues/29

https://leetcode.com/problems/divide-two-integers/

https://leetcode.com/problems/divide-two-integers/discuss/13524/summary-of-3-c-solutions

https://leetcode.com/problems/divide-two-integers/discuss/13407/C%2B%2B-bit-manipulations

https://leetcode.com/problems/divide-two-integers/discuss/142849/C%2B%2BJavaPython-Should-Not-Use-%22long%22-Int

LeetCode All in One 题目讲解汇总(持续更新中...)

## [LeetCode] Divide Two Integers 两数相除的更多相关文章

1. [LeetCode] 29. Divide Two Integers 两数相除

Given two integers dividend and divisor, divide two integers without using multiplication, division ...

2. 029 Divide Two Integers 两数相除

不使用乘号,除号和取模符号将两数相除.如果溢出返回 MAX_INT.详见:https://leetcode.com/problems/divide-two-integers/description/ ...

3. [LeetCode]29. Divide Two Integers两数相除

Given two integers dividend and divisor, divide two integers without using multiplication, division ...

4. [LintCode] Divide Two Integers 两数相除

Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...

5. [leetcode]29. Divide Two Integers两整数相除

Given two integers dividend and divisor, divide two integers without using multiplication, divisio ...

6. 【LeetCode每天一题】Divide Two Integers(两整数相除)

Given two integers dividend and divisor, divide two integers without using multiplication, division ...

7. [leetcode]29. Divide Two Integers 两整数相除

Given two integers dividend and divisor, divide two integers without using multiplication, division ...

8. LeetCode OJ：Divide Two Integers（两数相除）

Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...

9. [Swift]LeetCode29. 两数相除 | Divide Two Integers

Given two integers dividend and divisor, divide two integers without using multiplication, division ...

## 随机推荐

1. JS Select 月日日期联动

Js对Select控件进行联动操作,一个select选择月份后另一个select生成对应月份的所有日期. <%@ Page Language="C#" AutoEventWi ...

2. Resharp最新破解方法

ReSharper是一个JetBrains公司出品的著名的代码生成工具,其能帮助Microsoft Visual Studio成为一个更佳的IDE.它包括一系列丰富的能大大增加C#和Visual Ba ...

3. ubuntu /etc/network/interfaces 中配置虚拟链路

ubuntu /etc/network/interfaces 中配置虚拟链路 平常做一些关于网络的测试时,像一些需要在二层上运行的功能,一个网卡不足够的情况下,可使用 ip link 工具加一些虚拟的 ...

4. BootStrapt iCheck表单美化插件使用方法详解（含参数、事件等） 全选 反选

特色: 1.在不同浏览器(包括ie6+)和设备上都有相同的表现 — 包括 桌面和移动设备 2.支持触摸设备 — iOS.Android.BlackBerry.Windows Phone等系统 4.方便 ...

5. Sqli-labs less 43

Less-43 本关与42关的原理基本一致,我们还是定位在login.php中的password.看一下sql语句为: \$sql = "SELECT * FROM users WHERE u ...

6. 一个tomcat上放多个webapp问题，那这多个webapp会不会竞争端口呢？不会！安全两码事

1.一个tomcat上放多个webapp问题,那这多个webapp会不会竞争端口呢?不会!安全两码事

7. flexbox父盒子align-items属性

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

8. 线性表中顺序表的的理解和实现(java)

线性表的顺序表示指的是用一组地址连续的存储单元以此存储线性表的数据元素,这种表示也称作线性表的顺序存储结构或顺序映像.通常,称这种存储结构的线性表为顺序表.特点是:逻辑上相邻的数据元素,其物理次序上也 ...

9. Python实践练习：多重剪贴板

题目 假定你有一个无聊的任务,要填充一个网页或软件中的许多表格,其中包含一些文本字段.剪贴板让你不必一次又一次输入同样的文本,但剪贴板上一次只有一个内容.如果你有几段不同的文本需要拷贝粘贴,就不得不一 ...

10. POJ3261：Milk Patterns——题解

http://poj.org/problem?id=3261 给一个序列,求至少出现 k 次的最长重复子串,这 k 个子串可以重叠. 论文题+傻逼题. 上一道题(POJ1743)会做即可. 还是二分长 ...