29. Divide Two Integers

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
80
81
class Solution {
public int divide(int dividend, int divisor) {
// 解题思路:使用二分搜索寻找最接近的商
// 定义 z = x / y, 我们需要计算出 z, 为了防止溢出,必须对一些特例进行处理
int x = dividend, y = divisor;

// 对于 32 位有符号整数,下界为 -2^31, 上界为 2^31 - 1, 即对于边界时的数字部分,负数要比正数大 1
// 在符号不相同时,将正数转换为负数不会发生越界,而负数转换为正数可能发生越界,所以我们统一把 x 和 y 转换为负数进行计算以便求 z
// 在转换之前我们需要处理 -2^31 这个特殊的数
if (x == Integer.MIN_VALUE) {
if (y == -1) {
// 当被除数为 -2^31, 除数为 -1 时 z = 2^31, 此时无法用正数进行表示,会触发越界
return Integer.MAX_VALUE;
}
if (y == 1) {
// 当被除数为 -2^31, 除数为 1 时 z = -2^31, 此时提前返回,以便于后续在 [1, Integer.MAX_VALUE] 中搜索 z
return Integer.MIN_VALUE;
}
}

if (y == Integer.MIN_VALUE) {
// 除了自身没有其他数能除尽 -2^31
return x == Integer.MIN_VALUE ? 1 : 0;
}

int sign = 1;

if (x > 0) {
x = -x;
sign = -sign;
}
if (y > 0) {
y = -y;
sign = -sign;
}

// 现在 x 与 y 均为负数,z * y >= x > (z + 1) * y, 我们寻找最小的 z * y, 即寻找最大的 z, 此时使用二分搜索一步步缩小范围
int left = 1, right = Integer.MAX_VALUE; // [left, right]
int res = 0;
while (left <= right) {
int mid = (left + right) >>> 1;
if (productGreaterOrEqualTo(mid, y, x)) {
res = mid; // 记录下最后一个满足条件的 z
// 注意处理 mid 的越界
if (mid == Integer.MAX_VALUE) {
break;
}
left = mid + 1;
} else {
right = mid - 1;
}
}

return sign > 0 ? res : -res;
}

private boolean productGreaterOrEqualTo(int z, int y, int x) {
// 判断 z * y >= x 是否满足,其中 z 大于 0, x 和 y 都小于 0
// 因为不能使用乘法,所以使用加法计算

int res = 0;
while (z > 0) {
if ((z & 1) == 1) {
// z 为奇数,直接累加 y 至 res 上,累加前判断是否越界
if (res < x - y) {
return false;
}
res += y;
}
if (z > 1) {
if (y < x - y) {
return false;
}
y <<= 1;
}
z >>= 1;
}

return true;
}
}

Binary Search

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
class Solution {
public int divide(int dividend, int divisor) {
// 定义 z = x / y, 我们需要计算出 z
int x = dividend, y = divisor;

// 处理越界相关情况
if (x == Integer.MIN_VALUE) {
if (y == 1) {
return Integer.MIN_VALUE;
}
if (y == -1) {
return Integer.MAX_VALUE;
}
}

if (y == Integer.MIN_VALUE) {
return x == Integer.MIN_VALUE ? 1 : 0;
}

if (x == 0) {
return 0;
}

// 统一翻转为负数避免越界
int sign = 1;
if (x > 0) {
x = -x;
sign *= -1;
}
if (y > 0) {
y = -y;
sign *= -1;
}

List<Integer> valueList = new ArrayList<>();
valueList.add(y); // 注意此时 y 为负数
while (valueList.get(valueList.size() - 1) >= x - valueList.get(valueList.size() - 1)) {
valueList.add(valueList.get(valueList.size() - 1) << 1);
}

int res = 0;
for (int i = valueList.size() - 1; i >= 0; i--) {
if (x <= valueList.get(i)) {
res += 1 << i;
x -= valueList.get(i);
}
}

return sign * res;
}
}

References

29. Divide Two Integers
剑指 Offer II 001. 整数除法