Skip to content

Commit 2f6a9fb

Browse files
committed
feat: update solutions to lcof problems
更新剑指Offer题解
1 parent 412dfc4 commit 2f6a9fb

File tree

5 files changed

+44
-38
lines changed

5 files changed

+44
-38
lines changed

lcof/面试题14- I. 剪绳子/README.md

+16-14
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626

2727
## 解法
2828

29+
尽可能将绳子以长度 3 等分剪为多段时,乘积最大。
30+
2931
<!-- tabs:start -->
3032

3133
### **Python3**
@@ -35,28 +37,28 @@ class Solution:
3537
def cuttingRope(self, n: int) -> int:
3638
if n < 4:
3739
return n - 1
38-
s1, m = divmod(n, 3)
39-
if m == 1:
40-
s1 -= 1
41-
m = 4
42-
return pow(3, s1) * (1 if m == 0 else m)
40+
res = 1
41+
while n > 4:
42+
res *= 3
43+
n -= 3
44+
if n == 4:
45+
return res << 2
46+
return res * n
4347
```
4448

4549
### **Java**
4650

4751
```java
4852
class Solution {
4953
public int cuttingRope(int n) {
50-
if (n < 4) {
51-
return n - 1;
52-
}
53-
int s1 = n / 3;
54-
int m = n % 3;
55-
if (m == 1) {
56-
s1 -= 1;
57-
m = 4;
54+
if (n < 4) return n - 1;
55+
int res = 1;
56+
while (n > 4) {
57+
res *= 3;
58+
n -= 3;
5859
}
59-
return (int) (Math.pow(3, s1) * ((m == 0) ? 1 : m));
60+
if (n == 4) return res << 2;
61+
return res * n;
6062
}
6163
}
6264
```
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
class Solution {
22
public int cuttingRope(int n) {
3-
if (n < 4) {
4-
return n - 1;
3+
if (n < 4) return n - 1;
4+
int res = 1;
5+
while (n > 4) {
6+
res *= 3;
7+
n -= 3;
58
}
6-
int s1 = n / 3;
7-
int m = n % 3;
8-
if (m == 1) {
9-
s1 -= 1;
10-
m = 4;
11-
}
12-
return (int) (Math.pow(3, s1) * ((m == 0) ? 1 : m));
9+
if (n == 4) return res << 2;
10+
return res * n;
1311
}
1412
}

lcof/面试题14- I. 剪绳子/Solution.py

+7-5
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ class Solution:
22
def cuttingRope(self, n: int) -> int:
33
if n < 4:
44
return n - 1
5-
s1, m = divmod(n, 3)
6-
if m == 1:
7-
s1 -= 1
8-
m = 4
9-
return pow(3, s1) * (1 if m == 0 else m)
5+
res = 1
6+
while n > 4:
7+
res *= 3
8+
n -= 3
9+
if n == 4:
10+
return res << 2
11+
return res * n

lcof/面试题14- II. 剪绳子 II/README.md

+7-5
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,13 @@ class Solution:
3737
def cuttingRope(self, n: int) -> int:
3838
if n < 4:
3939
return n - 1
40-
s1, m = divmod(n, 3)
41-
if m == 1:
42-
s1 -= 1
43-
m = 4
44-
return (pow(3, s1) * (1 if m == 0 else m)) % 1000000007
40+
res = 1
41+
while n > 4:
42+
res *= 3
43+
n -= 3
44+
if n == 4:
45+
return (res << 2) % 1000000007
46+
return (res * n) % 1000000007
4547
```
4648

4749
### **Java**

lcof/面试题14- II. 剪绳子 II/Solution.py

+7-5
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ class Solution:
22
def cuttingRope(self, n: int) -> int:
33
if n < 4:
44
return n - 1
5-
s1, m = divmod(n, 3)
6-
if m == 1:
7-
s1 -= 1
8-
m = 4
9-
return (pow(3, s1) * (1 if m == 0 else m)) % 1000000007
5+
res = 1
6+
while n > 4:
7+
res *= 3
8+
n -= 3
9+
if n == 4:
10+
return (res << 2) % 1000000007
11+
return (res * n) % 1000000007

0 commit comments

Comments
 (0)