File tree 5 files changed +44
-38
lines changed
5 files changed +44
-38
lines changed Original file line number Diff line number Diff line change 26
26
27
27
## 解法
28
28
29
+ 尽可能将绳子以长度 3 等分剪为多段时,乘积最大。
30
+
29
31
<!-- tabs:start -->
30
32
31
33
### ** Python3**
@@ -35,28 +37,28 @@ class Solution:
35
37
def cuttingRope (self , n : int ) -> int :
36
38
if n < 4 :
37
39
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
43
47
```
44
48
45
49
### ** Java**
46
50
47
51
``` java
48
52
class Solution {
49
53
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 ;
58
59
}
59
- return (int ) (Math . pow(3 , s1) * ((m == 0 ) ? 1 : m));
60
+ if (n == 4 ) return res << 2 ;
61
+ return res * n;
60
62
}
61
63
}
62
64
```
Original file line number Diff line number Diff line change 1
1
class Solution {
2
2
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 ;
5
8
}
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 ;
13
11
}
14
12
}
Original file line number Diff line number Diff line change @@ -2,8 +2,10 @@ class Solution:
2
2
def cuttingRope (self , n : int ) -> int :
3
3
if n < 4 :
4
4
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
Original file line number Diff line number Diff line change @@ -37,11 +37,13 @@ class Solution:
37
37
def cuttingRope (self , n : int ) -> int :
38
38
if n < 4 :
39
39
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
45
47
```
46
48
47
49
### ** Java**
Original file line number Diff line number Diff line change @@ -2,8 +2,10 @@ class Solution:
2
2
def cuttingRope (self , n : int ) -> int :
3
3
if n < 4 :
4
4
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
You can’t perform that action at this time.
0 commit comments