-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdivide.go
More file actions
42 lines (35 loc) · 821 Bytes
/
Copy pathdivide.go
File metadata and controls
42 lines (35 loc) · 821 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
33
34
35
36
37
38
39
40
41
42
/* https://leetcode.com/problems/divide-two-integers/#/description
Divide two integers without using multiplication, division and mod operator.
If it is overflow, return MAX_INT.
*/
package lmath
func divide(dividend int, divisor int) int {
// 参考https://discuss.leetcode.com/category/37/divide-two-integers
if dividend == -2147483648 && divisor == -1 {
return 2147483647
}
sign := true
if (dividend < 0 && divisor > 0) || (dividend > 0 && divisor < 0) {
sign = false
}
if dividend < 0 {
dividend = -dividend
}
if divisor < 0 {
divisor = -divisor
}
res := 0
for dividend >= divisor {
temp, multiple := divisor, 1
for (temp << 1) < dividend {
temp = temp << 1
multiple = multiple << 1
}
dividend = dividend - temp
res += multiple
}
if !sign {
return -res
}
return res
}