-
Notifications
You must be signed in to change notification settings - Fork 0
/
LeetCode29.java
79 lines (75 loc) · 2.45 KB
/
LeetCode29.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
public class LeetCode29 {
public static void main(String[] args) {
LeetCode29 leetcode = new LeetCode29();
System.out.println(leetcode.divide(-2147483648, -1));
}
public int divide(int dividend, int divisor) {
return (int) divideLong(dividend, divisor);
}
private long divideLong(long dividend, long divisor) {
long counter = 0;
boolean negative = (dividend > 0 && divisor < 0) || (dividend < 0 && divisor > 0);
dividend = Math.abs(dividend);
divisor = Math.abs(divisor);
if (dividend < divisor) {
return 0;
}
if (divisor == 1) {
long result = negative ? -dividend : dividend;
if (result > Integer.MAX_VALUE) {
return Integer.MAX_VALUE;
}
if (result < Integer.MIN_VALUE) {
return Integer.MIN_VALUE;
}
return result;
}
// can be improved with bit shifting, which enables dividing by 2 without using division
// while (dividend >= 0) {
// dividend -= divisor;
// counter++;
// }
while (dividend >= divisor) {
int exponent = 0;
while (dividend >= (divisor << (exponent + 1))) {
exponent++;
}
counter += (1L << exponent);
dividend -= divisor << exponent;
}
long result = negative ? -counter : counter;
if (result > Integer.MAX_VALUE) {
return Integer.MAX_VALUE;
}
if (result < Integer.MIN_VALUE) {
return Integer.MIN_VALUE;
}
return result;
}
}
// Stolen solution
// public int divide(int dividend, int divisor) {
// if(divisor == dividend) return 1;
//
// boolean sign = true;
// if(dividend <= 0 && divisor > 0) sign = false;
// else if(dividend >= 0 && divisor < 0) sign = false;
//
// long a = dividend;
// a = Math.abs(a);
// long b = divisor;
// b = Math.abs(b);
// long quotient = 0;
// while(a >= b){
// int count = 0;
// while( a >= (b<<(count+1))){
// count += 1;
// }
// quotient += 1<<count;
// a = a - (b<<count);
// }
// if(quotient == (1<<31) && sign) return (1<<31)-1;
// if(quotient == (1<<31) && !sign) return -(1<<31);
//
// return (sign) ? (int)quotient : -(int)quotient;
// }