From 43d28e6bd3fa92f45a18dc00da7cd2470e0872f7 Mon Sep 17 00:00:00 2001 From: markwang Date: Wed, 7 Aug 2024 17:06:16 +0800 Subject: [PATCH] =?UTF-8?q?343.=E6=95=B4=E6=95=B0=E6=8B=86=E5=88=86?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0Go=E8=B4=AA=E5=BF=83=E8=A7=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0343.整数拆分.md | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/problems/0343.整数拆分.md b/problems/0343.整数拆分.md index 205b2201..7295627f 100644 --- a/problems/0343.整数拆分.md +++ b/problems/0343.整数拆分.md @@ -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) {