-
Notifications
You must be signed in to change notification settings - Fork 121
/
Copy pathDivideTwoIntegers.java
39 lines (36 loc) · 1.08 KB
/
DivideTwoIntegers.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
37
38
39
import java.math.BigDecimal;
import java.util.ArrayList;
/** Divide two integers without using multiplication, division and mod operator. */
public class DivideTwoIntegers {
public int divide(int dividend, int divisor) {
if (dividend == 0) return 0;
ArrayList<Integer> list = new ArrayList<Integer>();
BigDecimal d0 = new BigDecimal(dividend);
d0 = d0.abs();
BigDecimal d1;
BigDecimal d3 = new BigDecimal(divisor);
d3 = d3.abs();
int count = 0;
boolean start = true;
while (d0.compareTo(d3) == 1 || d0.compareTo(d3) == 0) {
d1 = new BigDecimal(divisor);
d1 = d1.abs();
start = true;
while (d0.compareTo(d1) == 1 || d0.compareTo(d1) == 0) {
if (start) {
list.add(1);
start = false;
} else {
int last = list.get(list.size() - 1);
list.add(last + last);
}
d0 = d0.subtract(d1);
d1 = d1.add(d1);
}
}
for (int e : list) {
count += e;
}
return (dividend < 0 && divisor < 0) || (dividend > 0 && divisor > 0) ? count : -count;
}
}