mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 00:43:04 +08:00
Merge pull request #2674 from markwang1992/343-integerBreak
343.整数拆分增加Go贪心解法
This commit is contained in:
@ -309,6 +309,8 @@ class Solution:
|
||||
|
||||
```
|
||||
### Go
|
||||
|
||||
动态规划
|
||||
```go
|
||||
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
|
||||
var integerBreak = function(n) {
|
||||
|
Reference in New Issue
Block a user