-
Notifications
You must be signed in to change notification settings - Fork 14
/
DivideTwoInteger.java
36 lines (35 loc) · 1.07 KB
/
DivideTwoInteger.java
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
public class Solution {
public int divide(int dividend, int divisor) {
// Start typing your Java solution below
// DO NOT write main() function
int a = Math.abs(dividend);
int b = Math.abs(divisor);
boolean neg = (dividend > 0 && divisor < 0) || (dividend < 0 && divisor > 0);
if (divisor == 0) {
return Integer.MAX_VALUE;
}
if (divisor == Integer.MIN_VALUE) {
return (dividend == Integer.MIN_VALUE) ? 1 : 0;
}
if (dividend == Integer.MIN_VALUE) {
if (neg) {
return -1 + divide(dividend + b, b);
}
else {
return 1 - divide(dividend + b, b);
}
}
int product = b, result = 0;
while (a >= b) {
int q = 1;
while (a - product >= product) {
q = q << 1;
product = product << 1;
}
a = a - product;
product = b;
result += q;
}
return (neg) ? -result : result;
}
}