-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathleetcode29_divide_two_integers.cpp
More file actions
98 lines (82 loc) · 2.34 KB
/
leetcode29_divide_two_integers.cpp
File metadata and controls
98 lines (82 loc) · 2.34 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/**
* @file leetcode29_divide_two_integers.cpp
* @author wangguibao(https://github.com/wangguibao)
* @date 2023/05/26 14:31
* @brief https://leetcode.com/problems/divide-two-integers/
*
**/
#include <iostream>
#include <map>
#include <cmath>
class Solution {
public:
int divide(int dividend, int divisor) {
long long int dividend_ll = dividend;
long long int divisor_ll = divisor;
if (divisor == 1) {
return safe_check(dividend_ll);
}
if (divisor == -1) {
return safe_check(0ll - dividend_ll);
}
bool diff_sign = false;
if ((dividend < 0 && divisor > 0) || (dividend > 0 && divisor < 0)) {
diff_sign = true;
}
if (dividend < 0) {
dividend_ll = 0 - dividend_ll;
}
if (divisor < 0) {
divisor_ll = 0 - divisor_ll;
}
if (dividend_ll < divisor_ll) {
return 0;
}
int shift = 0;
while ((divisor_ll << shift) <= dividend_ll) {
++shift;
}
--shift;
long long newdivisor = (divisor_ll << shift);
long long int quotient = 0;
long long int remain = dividend_ll;
while (remain > 0 && shift >= 0) {
newdivisor = (divisor_ll << shift);
remain -= newdivisor;
if (remain >= 0) {
quotient += (1 << (shift));
} else {
remain += newdivisor;
--shift;
}
}
if (diff_sign) {
quotient = 0 - quotient;
}
return safe_check(quotient);
}
private:
int safe_check(long long int x) {
if (x <= -0x80000000ll) {
return -0x80000000;
} else if (x > -0x80000000ll && x <= 0x7fffffffll) {
return x;
} else {
return 0x7fffffff;
}
}
};
int main() {
while (1) {
std::cout << "Input dividend and divisor (control-C to exit): ";
int dividend = 0;
int divisor = 0;
if (!(std::cin >> dividend >> divisor)) {
return 0;
}
Solution solution;
auto ret = solution.divide(dividend, divisor);
std::cout << ret << std::endl;
}
return 0;
}