-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleetcode_29.cpp
More file actions
36 lines (30 loc) · 1.1 KB
/
Copy pathleetcode_29.cpp
File metadata and controls
36 lines (30 loc) · 1.1 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
class Solution {
public:
int divide(int dividend, int divisor) {
// Handle edge cases
if (divisor == 0) throw invalid_argument("Division by zero is not allowed");
if (dividend == 0) return 0;
// Handle overflow case
if (dividend == INT_MIN && divisor == -1) return INT_MAX;
// Determine the sign of the result
bool isNegative = (dividend < 0) ^ (divisor < 0);
// Work with positive values
long long absDividend = abs((long long)dividend);
long long absDivisor = abs((long long)divisor);
long long quotient = 0;
// Perform repeated subtraction using bit shifts
while (absDividend >= absDivisor) {
long long currentDivisor = absDivisor, numShifts = 1;
while (absDividend >= (currentDivisor << 1)) {
currentDivisor <<= 1;
numShifts <<= 1;
}
absDividend -= currentDivisor;
quotient += numShifts;
}
// Apply the sign to the result
quotient = isNegative ? -quotient : quotient;
// Clamp the result to 32-bit integer range
return max(INT_MIN, min(INT_MAX, (int)quotient));
}
};