-
Notifications
You must be signed in to change notification settings - Fork 387
Expand file tree
/
Copy paths1.cpp
More file actions
28 lines (28 loc) · 856 Bytes
/
s1.cpp
File metadata and controls
28 lines (28 loc) · 856 Bytes
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
// OJ: https://leetcode.com/problems/divide-two-integers/
// Author: github.com/lzl124631x
// Time: O(logD) where D is |dividend|
// Space: O(1)
class Solution {
public:
int divide(int dividend, int divisor) {
if (!divisor) return 0; // divide-by-zero error
bool pos1 = dividend > 0, pos2 = divisor > 0, pos = !(pos1^pos2);
if (pos1) dividend = -dividend;
if (pos2) divisor = -divisor;
int q = 0, d = divisor, t = 1;
while (t > 0 && dividend < 0) {
if (dividend - d <= 0) {
dividend -= d;
q -= t;
if ((INT_MIN >> 1) < d) {
t <<= 1;
d <<= 1;
}
} else {
d >>= 1;
t >>= 1;
}
}
return pos? -q : q;
}
};