Repository navigation
Expand file tree
/
Copy pathP29_Divide_Two_Integers.java
More file actions
57 lines (53 loc) · 1.81 KB
/
Copy pathP29_Divide_Two_Integers.java
File metadata and controls
57 lines (53 loc) · 1.81 KB
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
package solution.medium;
/**
* @author Yi-Lo
* 2021/1/22 21:50
* @version 1.0
* @topics Math、Binary Search
* @urllink https://leetcode-cn.com/problems/divide-two-integers/
*/
public class P29_Divide_Two_Integers {
public static int divide_s1(int dividend, int divisor) {
boolean sign = (dividend > 0) ^ (divisor > 0);
int result = 0;
if (dividend > 0) dividend = -dividend;
if (divisor > 0) divisor = -divisor;
while (dividend <= divisor) {
int temp_result = -1;
int temp_divisor = divisor;
while (dividend <= (temp_divisor << 1)) {
if (temp_divisor <= (Integer.MIN_VALUE >> 1)) break;
temp_result = temp_result << 1;
temp_divisor = temp_divisor << 1;
}
dividend = dividend - temp_divisor;
result += temp_result;
}
if (!sign) {
if (result <= Integer.MIN_VALUE) return Integer.MAX_VALUE;
result = -result;
}
return result;
}
public static int divide_s2(int dividend, int divisor) {
if (dividend == 0) return 0;
if (dividend == Integer.MIN_VALUE && divisor == -1) {
return Integer.MAX_VALUE;
}
boolean negative = (dividend ^ divisor) < 0;
long temp_dividend = Math.abs((long) dividend);
long temp_divisor = Math.abs((long) divisor);
int result = 0;
for (int i = 31; i >= 0; i--) {
if ((temp_dividend >> i) >= temp_divisor) {
result += 1 << i;
temp_dividend -= temp_divisor << i;
}
}
return negative ? -result : result;
}
public static void main(String[] args) {
System.out.println(divide_s1(10, 3));
System.out.println(divide_s2(10, 3));
}
}