-
Notifications
You must be signed in to change notification settings - Fork 548
/
divide-two-integers.go
executable file
·84 lines (68 loc) · 1.33 KB
/
divide-two-integers.go
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
82
83
84
package Problem0029
import (
"math"
)
func divide(m, n int) int {
// 防止有人把0当做除数
if n == 0 {
return math.MaxInt32
}
signM, absM := analysis(m)
signN, absN := analysis(n)
res, _ := d(absM, absN, 1)
// 修改res的符号
if signM != signN {
res = res - res - res
}
// 检查溢出
if res < math.MinInt32 || res > math.MaxInt32 {
return math.MaxInt32
}
return res
}
func analysis(num int) (sign, abs int) {
sign = 1
abs = num
if num < 0 {
sign = -1
abs = num - num - num
}
return
}
// d 计算m/n的值,返回结果和余数
// m >= 0
// n > 0
// count == 1, 代表初始n的个数,在递归过程中,count == 当前的n/初始的n
func d(m, n, count int) (res, remainder int) {
switch {
case m < n:
return 0, m
case n <= m && m < n+n:
return count, m - n
default:
res, remainder = d(m, n+n, count+count)
if remainder >= n {
return res + count, remainder - n
}
return
}
}
// 以下为上述递归方法的普通实现方式
// func d(m, n int) int {
// res := 0
// rs, ress := []int{n}, []int{1}
// temp, i := n+n, 1
// for temp <= m {
// rs = append(rs, temp)
// ress = append(ress, ress[i-1]+ress[i-1])
// temp += temp
// i++
// }
// for i := len(rs) - 1; i >= 0; i-- {
// if m >= rs[i] {
// m -= rs[i]
// res += ress[i]
// }
// }
// return res
// }