- Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathSolution29.java
More file actions
Latest commit
executable file
·32 lines (29 loc) · 901 Bytes
/
Copy pathSolution29.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
publicclassSolution29 {
publicintdivide(intdividend, intdivisor) {
if (divisor == 0) return0;
if (dividend == Integer.MIN_VALUE && divisor == -1) {
returnInteger.MAX_VALUE;
}
booleanlabel;
if (dividend * divisor > 0) {
label = true;
} else {
label = false;
}
longdividendL = Math.abs((long) dividend);
longdivisorL = Math.abs((long) divisor);
intresult = 0;
for (inti = 31; i >= 0; i--) {
if ((dividendL >> i) >= divisorL) {
result += 1 << i;
dividendL -= divisorL << i;
}
}
result = label ? result : -result;
returnresult;
}
publicstaticvoidmain(String[] args) {
Solution29s = newSolution29();
System.out.println(s.divide(214748368,-1));
}
}