Skip to content

Commit e1b9902

Browse files
author
chensl
committed
feat: add solutions to lcof2 problem: No.001
1 parent a9fa871 commit e1b9902

File tree

4 files changed

+179
-1
lines changed

4 files changed

+179
-1
lines changed

lcof2/剑指 Offer II 001. 整数除法/README.md

+98-1
Original file line numberDiff line numberDiff line change
@@ -64,22 +64,119 @@
6464

6565
<!-- 这里可写通用的实现逻辑 -->
6666

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+
6780
<!-- tabs:start -->
6881

6982
### **Python3**
7083

7184
<!-- 这里可写当前语言的特殊实现逻辑 -->
7285

7386
```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
75103
```
76104

77105
### **Java**
78106

79107
<!-- 这里可写当前语言的特殊实现逻辑 -->
80108

81109
```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+
```
82143

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+
}
83180
```
84181

85182
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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 numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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 numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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

0 commit comments

Comments
 (0)