-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathSolution29.java
More file actions
executable file
·32 lines (29 loc) · 901 Bytes
/
Solution29.java
File metadata and controls
executable file
·32 lines (29 loc) · 901 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
30
31
32
public class Solution29 {
public int divide(int dividend, int divisor) {
if (divisor == 0) return 0;
if (dividend == Integer.MIN_VALUE && divisor == -1) {
return Integer.MAX_VALUE;
}
boolean label;
if (dividend * divisor > 0) {
label = true;
} else {
label = false;
}
long dividendL = Math.abs((long) dividend);
long divisorL = Math.abs((long) divisor);
int result = 0;
for (int i = 31; i >= 0; i--) {
if ((dividendL >> i) >= divisorL) {
result += 1 << i;
dividendL -= divisorL << i;
}
}
result = label ? result : -result;
return result;
}
public static void main(String[] args) {
Solution29 s = new Solution29();
System.out.println(s.divide(214748368,-1));
}
}