-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path29.divide-two-integers.go
146 lines (133 loc) · 2.87 KB
/
29.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
* @lc app=leetcode id=29 lang=golang
*
* [29] Divide Two Integers
*
* https://leetcode.com/problems/divide-two-integers/description/
*
* algorithms
* Medium (16.42%)
* Likes: 1279
* Dislikes: 5497
* Total Accepted: 293.6K
* Total Submissions: 1.8M
* Testcase Example: '10\n3'
*
* Given two integers dividend and divisor, divide two integers without using
* multiplication, division and mod operator.
*
* Return the quotient after dividing dividend by divisor.
*
* The integer division should truncate toward zero, which means losing its
* fractional part. For example, truncate(8.345) = 8 and truncate(-2.7335) =
* -2.
*
* Example 1:
*
*
* Input: dividend = 10, divisor = 3
* Output: 3
* Explanation: 10/3 = truncate(3.33333..) = 3.
*
*
* Example 2:
*
*
* Input: dividend = 7, divisor = -3
* Output: -2
* Explanation: 7/-3 = truncate(-2.33333..) = -2.
*
*
* Note:
*
*
* Both dividend and divisor will be 32-bit signed integers.
* The divisor will never be 0.
* Assume we are dealing with an environment which could only store integers
* within the 32-bit signed integer range: [−2^31, 2^31 − 1]. For the purpose
* of this problem, assume that your function returns 2^31 − 1 when the
* division result overflows.
*
*
*/
// @lc code=start
func divide(dividend int, divisor int) int {
return divide2(dividend, divisor)
}
// test case: 1\n-1
func divide2(dividend int, divisor int) int {
minInt32, maxInt32 := -1<<31, 1<<31-1
if dividend == 0 {
return 0
}
if divisor == 1 {
return dividend
}
if dividend == minInt32 && divisor == -1 {
return maxInt32
}
isPostive := true
if dividend < 0 {
dividend = -dividend
isPostive = !isPostive
}
if divisor < 0 {
divisor = -divisor
isPostive = !isPostive
}
// divide some part to approach the answer
quotient := div(dividend, divisor)
if !isPostive {
return -quotient
}
return quotient
}
// similar binary search
func div(val1 int, val2 int) int {
if val1 < val2 {
return 0
}
count, divisor := 1, val2
for (divisor + divisor) < val1 {
count += count // double
divisor += divisor // double
}
return count + div(val1-divisor, val2)
}
// test case: 1\n-1
func divide1(dividend int, divisor int) int {
minInt32, maxInt32 := -1<<31, 1<<31-1
if dividend == 0 {
return 0
}
if divisor == 1 {
return dividend
}
if dividend == minInt32 && divisor == -1 {
return maxInt32
}
isPostive := true
if dividend < 0 {
dividend = -dividend
isPostive = !isPostive
}
if divisor < 0 {
divisor = -divisor
isPostive = !isPostive
}
// divide some part to approach the answer
quotient := 0
for dividend >= divisor { // todo: thinking, why for iterator
multiple := divisor
for i := 0; dividend >= multiple; i++ {
dividend -= multiple
quotient += 1 << uint(i)
multiple <<= 1
}
}
if !isPostive {
return -quotient
}
return quotient
}
// @lc code=end