-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDivide_Two_Integer_29.java
More file actions
29 lines (25 loc) · 942 Bytes
/
Copy pathDivide_Two_Integer_29.java
File metadata and controls
29 lines (25 loc) · 942 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
package leetcode;
/**
* Created by sunbo_000 on 10/3/2016.
*/
public class Divide_Two_Integer_29 {
public int divide(int dividend, int divisor) {
if (divisor == 0) return Integer.MAX_VALUE;
if (dividend == Integer.MIN_VALUE && divisor == -1) return Integer.MAX_VALUE;
long dividend_pos = Math.abs((long) dividend);
long divisor_pos = Math.abs((long) divisor);
int result = 0;
while (dividend_pos >= divisor_pos) {
int tmp = 0;
while (dividend_pos >= (divisor_pos << tmp)) tmp++;
result += 1 << (tmp - 1);
dividend_pos -= (divisor_pos << (tmp - 1));
}
if ((dividend > 0 && divisor < 0) || (divisor > 0 && dividend < 0)) result = -result;
return result;
}
public static void main(String[] args) {
Divide_Two_Integer_29 Solution = new Divide_Two_Integer_29();
Solution.divide(-1,1);
}
}