mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-07 15:45:40 +08:00
343.整数拆分增加Go贪心解法
This commit is contained in:
@ -309,6 +309,8 @@ class Solution:
|
|||||||
|
|
||||||
```
|
```
|
||||||
### Go
|
### Go
|
||||||
|
|
||||||
|
动态规划
|
||||||
```go
|
```go
|
||||||
func integerBreak(n int) int {
|
func integerBreak(n int) int {
|
||||||
/**
|
/**
|
||||||
@ -338,6 +340,28 @@ func max(a, b int) int{
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
贪心
|
||||||
|
```go
|
||||||
|
func integerBreak(n int) int {
|
||||||
|
if n == 2 {
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
if n == 3 {
|
||||||
|
return 2
|
||||||
|
}
|
||||||
|
if n == 4 {
|
||||||
|
return 4
|
||||||
|
}
|
||||||
|
result := 1
|
||||||
|
for n > 4 {
|
||||||
|
result *= 3
|
||||||
|
n -= 3
|
||||||
|
}
|
||||||
|
result *= n
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
### Javascript
|
### Javascript
|
||||||
```Javascript
|
```Javascript
|
||||||
var integerBreak = function(n) {
|
var integerBreak = function(n) {
|
||||||
|
Reference in New Issue
Block a user