Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 6afccb6

Browse files
Merge pull request youngyangyang04#618 from X-shuffle/master
添加 0343.整数拆分 go版本
2 parents 2747c40 + 3f45f39 commit 6afccb6

File tree

1 file changed

+28
-1
lines changed

1 file changed

+28
-1
lines changed

‎problems/0343.整数拆分.md

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,34 @@ class Solution:
227227
return dp[n]
228228
```
229229
Go:
230-
230+
```golang
231+
func integerBreak(n int) int {
232+
/**
233+
动态五部曲
234+
1.确定dp下标及其含义
235+
2.确定递推公式
236+
3.确定dp初始化
237+
4.确定遍历顺序
238+
5.打印dp
239+
**/
240+
dp:=make([]int,n+1)
241+
dp[1]=1
242+
dp[2]=1
243+
for i:=3;i<n+1;i++{
244+
for j:=1;j<i-1;j++{
245+
// i可以差分为i-j和j。由于需要最大值,故需要通过j遍历所有存在的值,取其中最大的值作为当前i的最大值,在求最大值的时候,一个是j与i-j相乘,一个是j与dp[i-j].
246+
dp[i]=max(dp[i],max(j*(i-j),j*dp[i-j]))
247+
}
248+
}
249+
return dp[n]
250+
}
251+
func max(a,b int) int{
252+
if a>b{
253+
return a
254+
}
255+
return b
256+
}
257+
```
231258

232259
Javascript:
233260
```Javascript

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /