forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_29.java
54 lines (48 loc) · 1.35 KB
/
_29.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package com.fishercoder.solutions;
/**
* 29. Divide Two Integers
*
* Divide two integers without using multiplication, division and mod operator.
* If it is overflow, return MAX_INT.
*/
public class _29 {
public static class Solution1 {
public int divide(int dividend, int divisor) {
if (divisor == 0 || (dividend == Integer.MIN_VALUE && divisor == -1)) {
return Integer.MAX_VALUE;
}
if (dividend != Integer.MIN_VALUE
&& Math.abs(dividend) < Math.abs(divisor)) {
return 0;
}
if (divisor == Integer.MIN_VALUE) {
return (dividend == Integer.MIN_VALUE) ? 1 : 0;
}
boolean flag = (dividend < 0) ^ (divisor < 0);
dividend = -Math.abs(dividend);
divisor = -Math.abs(divisor);
int[] num = new int[40];
int[] multiple = new int[40];
num[1] = divisor;
multiple[1] = 1;
for (int i = 2; i < 32 && num[i - 1] < 0; ++i) {
num[i] = num[i - 1] << 1;
multiple[i] = multiple[i - 1] << 1;
}
int result = 0;
int index = 1;
while (num[index] < 0) {
++index;
}
index -= 1;
while (dividend <= divisor) {
while (dividend <= num[index]) {
result += multiple[index];
dividend -= num[index];
}
--index;
}
return !flag ? result : -result;
}
}
}