From 52d5e51b2a86d04d0202056d6bd7f51f5977d61c Mon Sep 17 00:00:00 2001 From: Omooo <869759698@qq.com> Date: Thu, 11 Jun 2020 10:14:05 +0800 Subject: [PATCH] =?UTF-8?q?Update=20=E6=95=B0=E7=BB=84=E7=9B=B8=E5=85=B3.m?= =?UTF-8?q?d?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- blogs/Algorithm/剑指 Offer/数组相关.md | 35 ++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/blogs/Algorithm/剑指 Offer/数组相关.md b/blogs/Algorithm/剑指 Offer/数组相关.md index 1dc8f9f..b787648 100644 --- a/blogs/Algorithm/剑指 Offer/数组相关.md +++ b/blogs/Algorithm/剑指 Offer/数组相关.md @@ -124,3 +124,38 @@ class Solution { } ``` +[42. 连续子数组的最大和](https://leetcode-cn.com/problems/lian-xu-zi-shu-zu-de-zui-da-he-lcof/) + +```java +class Solution { + + public int maxSubArray(int[] nums) { + int max = Integer.MIN_VALUE; + int sum = 0; + for (int num : nums) { + if (sum <= 0) { + sum = num; + } else { + sum += num; + } + max = Math.max(max, sum); + } + return max; + } +} +``` + +```java +class Solution { + + public int maxSubArray(int[] nums) { + int res = nums[0]; + for (int i = 1; i < nums.length; i++) { + nums[i] += Math.max(nums[i - 1], 0); + res = Math.max(res, nums[i]); + } + return res; + } +} +``` +