landscapemi的博客

# leetcode :Binary Search： Divide Two Integers(029)

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

If it is overflow, return MAX_INT.

### 算法

o（nlgn）


typedef long long ll;
class Solution {
public:
int divide(int dividend, int divisor) {
ll a = dividend >= 0 ? dividend : -(ll)dividend;
ll b = divisor >= 0 ? divisor : -(ll)divisor;
ll result = 0, c = 0;
bool sign = (dividend > 0 && divisor < 0) ||
(dividend < 0 && divisor > 0);

while (a >= b) {
c = b;
for (int i = 0; a >= c; i++, c <<= 1) {
a -= c;
result += (1<<i);
}
}
if (sign) {
return max((ll)INT_MIN, -result);
} else {
return min((ll)INT_MAX, result);
}
}
};

#### Divide Two Integers -- LeetCode

2014-02-27 05:41:54

#### LeetCode --- 29. Divide Two Integers

2015-02-02 21:58:27

#### (LeetCode) Divide Two Integers (Java)思路讲解及实现

2015-09-30 15:52:53

#### [Leetcode]29. Divide Two Integers @python

2016-01-16 16:48:08

#### LeetCode29DivideTwoIntegers--In Java

2016-02-22 20:24:54

#### LeetCode 29 Divide Two Integers (C,C++,Java,Python)

2015-05-13 09:27:20

#### leetcode 29 -- Divide Two Integers

2015-06-12 17:23:05

#### LeetCode(29)Divide Two Integers

2014-01-03 07:24:18

#### leetcode——Divide Two Integers 不用乘除取余操作求除法（AC）

2014-06-09 20:19:43

#### LeetCode 29 Divide Two Integers（两个整数相除）（*）

2015-11-17 20:04:53