forked from Ewenwan/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path029_DivideTwoIntegers.cpp
67 lines (62 loc) · 1.05 KB
/
029_DivideTwoIntegers.cpp
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
59
60
61
62
63
64
65
66
67
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
int divide(int dividend, int divisor) {
long long a = dividend;
long long b = divisor;
int sign = 1;
if (a == 0){
return 0;
}
else if (a < 0){
a = -a;
sign = -sign;
}
if (b == 0){
return INT_MAX;
} else if (b < 0){
b = -b;
sign = -sign;
}
if (a < b){
return 0;
}
long long val[33], times[33];
val[0] = b;
times[0] = 1;
int i = 0;
for (i = 1; i < 33; ++i){
val[i] = (val[i-1] << 1);
times[i] = (times[i-1] << 1);
if (val[i] > a){
break;
}
}
long long res = 0;
--i;
for (; i >= 0; --i){
if (a >= val[i]){
a -= val[i];
res += times[i];
}
}
if (sign == 1 && res >= INT_MAX){
return INT_MAX;
}
if (sign == -1){
res = -res;
if (res < INT_MIN){
return INT_MIN;
}
}
return res;
}
};
int main(int argc, char *argv[]){
Solution s;
cout << s.divide(-2147483648, -1) << endl;
system("pause");
return 0;
}