We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
2 parents 65c0c9a + 2b8918c commit cef8f03Copy full SHA for cef8f03
Algorithms/0343.integer-break/integer-break.go
@@ -1,5 +1,6 @@
1
package problem0343
2
3
+// https://blog.csdn.net/fuxuemingzhu/article/details/80486238
4
func integerBreak(n int) int {
5
if n == 2 {
6
return 1
@@ -9,26 +10,11 @@ func integerBreak(n int) int {
9
10
return 2
11
}
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)
+ res := 1
+ for n > 4 {
+ res *= 3
+ n -= 3
19
20
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
+ return res * n
34
0 commit comments