Skip to content
This repository was archived by the owner on Sep 20, 2023. It is now read-only.

Commit cef8f03

Browse files
authored
Merge pull request #55 from mlkr/develop
343 更快的解法
2 parents 65c0c9a + 2b8918c commit cef8f03

File tree

1 file changed

+6
-20
lines changed

1 file changed

+6
-20
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package problem0343
22

3+
// https://blog.csdn.net/fuxuemingzhu/article/details/80486238
34
func integerBreak(n int) int {
45
if n == 2 {
56
return 1
@@ -9,26 +10,11 @@ func integerBreak(n int) int {
910
return 2
1011
}
1112

12-
switch n % 3 {
13-
case 0:
14-
return pow3(n / 3)
15-
case 1:
16-
return 4 * pow3(n/3-1)
17-
default:
18-
return 2 * pow3(n/3)
13+
res := 1
14+
for n > 4 {
15+
res *= 3
16+
n -= 3
1917
}
2018

21-
}
22-
23-
func pow3(n int) int {
24-
if n == 0 {
25-
return 1
26-
}
27-
28-
res := pow3(n >> 1)
29-
if n&1 == 0 {
30-
return res * res
31-
}
32-
33-
return res * res * 3
19+
return res * n
3420
}

0 commit comments

Comments
 (0)