File tree 4 files changed +179
-1
lines changed
lcof2/剑指 Offer II 001. 整数除法
4 files changed +179
-1
lines changed Original file line number Diff line number Diff line change 64
64
65
65
<!-- 这里可写通用的实现逻辑 -->
66
66
67
+ 通过下面这段伪代码,不难理解除法本质上就是减法,但是一次循环只能做一次减法,效率太低会导致超时,所以再加上快速幂的思想优化即可
68
+
69
+ ``` py
70
+ sign = - 1 if a * b < 0 else 1
71
+ a = abs (a)
72
+ b = abs (b)
73
+ cnt = 0
74
+ while a >= b:
75
+ a -= b
76
+ cnt += 1
77
+ return sign * cnt
78
+ ```
79
+
67
80
<!-- tabs:start -->
68
81
69
82
### ** Python3**
70
83
71
84
<!-- 这里可写当前语言的特殊实现逻辑 -->
72
85
73
86
``` python
74
-
87
+ class Solution :
88
+ def divide (self , a : int , b : int ) -> int :
89
+ INT_MAX = (1 << 31 ) - 1
90
+ INT_MIN = - (1 << 31 )
91
+ sign = - 1 if a * b < 0 else 1
92
+ a = abs (a)
93
+ b = abs (b)
94
+ tot = 0
95
+ while a >= b:
96
+ cnt = 0
97
+ while a >= (b << cnt):
98
+ cnt += 1
99
+ cnt -= 1
100
+ tot += 1 << cnt
101
+ a -= b << cnt
102
+ return sign * tot if INT_MIN <= sign * tot <= INT_MAX else INT_MAX
75
103
```
76
104
77
105
### ** Java**
78
106
79
107
<!-- 这里可写当前语言的特殊实现逻辑 -->
80
108
81
109
``` java
110
+ class Solution {
111
+ public int divide (int a , int b ) {
112
+ int sign = 1 ;
113
+ if ((a < 0 ) != (b < 0 )) {
114
+ sign = - 1 ;
115
+ }
116
+ long x = abs(a);
117
+ long y = abs(b);
118
+ long tot = 0 ;
119
+ while (x >= y) {
120
+ int cnt = 0 ;
121
+ while (x >= (y << cnt)) {
122
+ cnt++ ;
123
+ }
124
+ cnt-- ;
125
+ tot += 1L << cnt;
126
+ x -= y << cnt;
127
+ }
128
+ long ans = sign * tot;
129
+ if (ans >= Integer . MIN_VALUE && ans <= Integer . MAX_VALUE ) {
130
+ return (int ) ans;
131
+ }
132
+ return Integer . MAX_VALUE ;
133
+ }
134
+
135
+ private long abs (long a ) {
136
+ if (a < 0 ) {
137
+ return - a;
138
+ }
139
+ return a;
140
+ }
141
+ }
142
+ ```
82
143
144
+ ### ** Go**
145
+
146
+ ``` go
147
+ func divide (a int , b int ) int {
148
+ sign := 1
149
+ if a*b < 0 {
150
+ sign = -1
151
+ }
152
+
153
+ a = abs (a)
154
+ b = abs (b)
155
+
156
+ tot := 0
157
+ for a >= b {
158
+ cnt := 0
159
+ for a >= (b << cnt) {
160
+ cnt++
161
+ }
162
+ cnt--
163
+ tot += 1 << cnt
164
+ a -= b << cnt
165
+ }
166
+
167
+ ans := sign * tot
168
+ if ans >= math.MinInt32 && ans <= math.MaxInt32 {
169
+ return ans
170
+ }
171
+ return math.MaxInt32
172
+ }
173
+
174
+ func abs (a int ) int {
175
+ if a < 0 {
176
+ return -a
177
+ }
178
+ return a
179
+ }
83
180
```
84
181
85
182
### ** ...**
Original file line number Diff line number Diff line change
1
+ func divide (a int , b int ) int {
2
+ sign := 1
3
+ if a * b < 0 {
4
+ sign = - 1
5
+ }
6
+
7
+ a = abs (a )
8
+ b = abs (b )
9
+
10
+ tot := 0
11
+ for a >= b {
12
+ cnt := 0
13
+ for a >= (b << cnt ) {
14
+ cnt ++
15
+ }
16
+ cnt --
17
+ tot += 1 << cnt
18
+ a -= b << cnt
19
+ }
20
+
21
+ ans := sign * tot
22
+ if ans >= math .MinInt32 && ans <= math .MaxInt32 {
23
+ return ans
24
+ }
25
+ return math .MaxInt32
26
+ }
27
+
28
+ func abs (a int ) int {
29
+ if a < 0 {
30
+ return - a
31
+ }
32
+ return a
33
+ }
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public int divide (int a , int b ) {
3
+ int sign = 1 ;
4
+ if ((a < 0 ) != (b < 0 )) {
5
+ sign = -1 ;
6
+ }
7
+ long x = abs (a );
8
+ long y = abs (b );
9
+ long tot = 0 ;
10
+ while (x >= y ) {
11
+ int cnt = 0 ;
12
+ while (x >= (y << cnt )) {
13
+ cnt ++;
14
+ }
15
+ cnt --;
16
+ tot += 1L << cnt ;
17
+ x -= y << cnt ;
18
+ }
19
+ long ans = sign * tot ;
20
+ if (ans >= Integer .MIN_VALUE && ans <= Integer .MAX_VALUE ) {
21
+ return (int ) ans ;
22
+ }
23
+ return Integer .MAX_VALUE ;
24
+ }
25
+
26
+ private long abs (long a ) {
27
+ if (a < 0 ) {
28
+ return -a ;
29
+ }
30
+ return a ;
31
+ }
32
+ }
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def divide (self , a : int , b : int ) -> int :
3
+ INT_MAX = (1 << 31 ) - 1
4
+ INT_MIN = - (1 << 31 )
5
+ sign = - 1 if a * b < 0 else 1
6
+ a = abs (a )
7
+ b = abs (b )
8
+ tot = 0
9
+ while a >= b :
10
+ cnt = 0
11
+ while a >= (b << cnt ):
12
+ cnt += 1
13
+ cnt -= 1
14
+ tot += 1 << cnt
15
+ a -= b << cnt
16
+ return sign * tot if INT_MIN <= sign * tot <= INT_MAX else INT_MAX
You can’t perform that action at this time.
0 commit comments