-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution.java
More file actions
29 lines (22 loc) · 766 Bytes
/
solution.java
File metadata and controls
29 lines (22 loc) · 766 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
29
class Solution {
public int divide(int dividend, int divisor) {
if (dividend == 0) {
return 0;
}
if (dividend == Integer.MIN_VALUE && divisor == -1) {
return Integer.MAX_VALUE;
}
int ans = 0;
boolean dds = dividend > 0, dss = divisor > 0, signed = dds ^ dss;
dividend = dds ? dividend : -dividend;
divisor = dss ? divisor : -divisor;
while (dividend - divisor >= 0) {
int mod = 0;
while(dividend - (divisor << mod << 1) >= 0)
mod++;
dividend -= divisor << mod;
ans += 1 << mod;
}
return signed ? -ans : ans;
}
}