blob: 7c172f7b2ddb5279dceb865fa68388253451e0d2 (
plain)
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
|
#include <bits/stdc++.h>
class Solution {
public:
int divide(int dividend, int divisor) {
if (dividend == divisor) {
if (divisor == -1)
return INT_MAX;
else if (divisor == INT_MIN)
return 1;
else if (divisor < 0)
return 1 + divide(dividend - divisor, divisor);
else
return -1 + divide(dividend + divisor, divisor);
} else if (divisor == INT_MIN)
return 0;
bool negative = (dividend < 0) ^ (divisor < 0);
dividend = abs(dividend);
divisor = abs(divisor);
int result = 0;
for (int i = 31; i >= 0; --i) {
int shiftedDiv = (dividend >> i);
if (shiftedDiv >= divisor) {
result += (1 << i);
dividend -= (divisor << i);
}
}
return (negative ? -result : result);
}
};
int main() {}
|