-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdivide_two_integers.cc
71 lines (59 loc) · 2.69 KB
/
divide_two_integers.cc
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
68
69
70
71
// O(lgN)
// Analyse: store divisor divisor+divisor divisor+divisor+divisor ... 65536
// compare dividend and 65536 65536+65536 65536+65536+65536
// Conclude a Method:
// Prestore long a[n]
// divisor * 2^0
// divisor * 2^1
// divisor * 2^2
// divisor * 2^4
// ... just more than the biggest int(2147483647).
// Copyright 2014 kevinew@leetcode Inc. All Rights Reserved.
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <iostream>
using namespace std;
class Solution {
public:
int divide(long long dividend, long long divisor) {
int ret, i;
int flag1 = 1, flag2 = 1;
if (dividend < 0) {
flag1 = -1;
dividend = 0 - dividend;
}
if (divisor < 0) {
flag2 = -1;
divisor = 0 - divisor;
}
vector<long long> a;
a.reserve(33);
a[0] = divisor;
for (i = 1; ; ++i) {
a[i] = a[i - 1] << 1;
if (a[i] > dividend) break;
}
--i;
int result = 0;
bool first = false;
while (i >= 0) {
result <<= 1;
if (dividend >= a[i]) {
result += 1;
dividend -= a[i];
}
--i;
}
if (flag1 + flag2 == 0) return -result;
else return result;
}
};
int main(int argc, char **argv) {
Solution s;
// When I change the args of divide from int to long long,
// then passed this case.
cout << s.divide(-1010369383, -2147483648) << endl;
return 0;
}