-
Notifications
You must be signed in to change notification settings - Fork 213
/
DivideTwoIntegers.h
58 lines (56 loc) · 1.69 KB
/
DivideTwoIntegers.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/*
Author: Annie Kim, [email protected] : King, [email protected]
Date: Jul 8, 2013
Update: Nov 18, 2014
Problem: Divide Two Integers
Difficulty: Medium
Source: https://oj.leetcode.com/problems/divide-two-integers/
Notes:
Divide two integers without using multiplication, division and mod operator.
Solution: Use << operator.
*/
class Solution {
public:
//Top -> Down
int divide_1(int dividend, int divisor) {
assert(divisor != 0);
bool flag = (dividend < 0) ^ (divisor < 0);
long long dividendll = abs((long long)dividend);
long long divisorll = abs((long long)divisor);
long long res = 0;
long long d = divisorll, q = 1;
while ((d << 1) <= dividendll) {
d <<= 1;
q <<= 1;
}
while (dividendll >= divisorll) {
if (dividendll >= d) {
dividendll -= d;
res += q;
}
d >>= 1;
q >>= 1;
}
if (flag == true) res = -res;
if (res > INT_MAX) return INT_MAX;
return res;
}
//bottom -> up
int divide(int dividend, int divisor) {
assert(divisor != 0);
bool flag = (dividend < 0) ^ (divisor < 0);
long long Dividend = abs((long long)dividend);
long long Divisor = abs((long long)divisor);
long long res = 0;
while (Dividend >= Divisor) {
long long c = Divisor;
for (int i = 0; (c << i) <= Dividend; ++i) {
Dividend -= (c << i);
res += (1 << i);
}
}
if (flag == true) res = -res;
if (res > INT_MAX) return INT_MAX;
return res;
}
};