From df8878b79192527f3d8617e09c93227340d03c10 Mon Sep 17 00:00:00 2001 From: gdstzmy <79707886+gdstzmy@users.noreply.github.com> Date: Tue, 20 Feb 2024 18:09:07 +0800 Subject: [PATCH] =?UTF-8?q?Update=200053.=E6=9C=80=E5=A4=A7=E5=AD=90?= =?UTF-8?q?=E5=BA=8F=E5=92=8C.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加go语言贪心法 --- problems/0053.最大子序和.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/problems/0053.最大子序和.md b/problems/0053.最大子序和.md index 78c8b382..74ff2ca4 100644 --- a/problems/0053.最大子序和.md +++ b/problems/0053.最大子序和.md @@ -230,7 +230,25 @@ class Solution: ``` ### Go +贪心法 +```go +func maxSubArray(nums []int) int { + max := nums[0] + count := 0 + for i := 0; i < len(nums); i++{ + count += nums[i] + if count > max{ + max = count + } + if count < 0 { + count = 0 + } + } + return max +} +``` +动态规划 ```go func maxSubArray(nums []int) int { maxSum := nums[0]