From f7fd83baf4ed0693ff9597a812d51d444552a434 Mon Sep 17 00:00:00 2001 From: CyC2018 <1029579233@qq.com> Date: Thu, 4 Oct 2018 13:54:39 +0800 Subject: [PATCH] auto commit --- notes/Leetcode 题解.md | 48 +++++++++++++++++++++--------------------- 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/notes/Leetcode 题解.md b/notes/Leetcode 题解.md index 960e3b1f..dd435f37 100644 --- a/notes/Leetcode 题解.md +++ b/notes/Leetcode 题解.md @@ -804,6 +804,30 @@ public int maxProfit(int[] prices) { } ``` +**子数组最大的和** + +[53. Maximum Subarray (Easy)](https://leetcode.com/problems/maximum-subarray/description/) + +```html +For example, given the array [-2,1,-3,4,-1,2,1,-5,4], +the contiguous subarray [4,-1,2,1] has the largest sum = 6. +``` + +```java +public int maxSubArray(int[] nums) { + if (nums == null || nums.length == 0) { + return 0; + } + int preSum = nums[0]; + int maxSum = preSum; + for (int i = 1; i < nums.length; i++) { + preSum = preSum > 0 ? preSum + nums[i] : nums[i]; + maxSum = Math.max(maxSum, preSum); + } + return maxSum; +} +``` + ## 二分查找 **正常实现** @@ -2586,30 +2610,6 @@ class NumArray { } ``` -**子数组最大的和** - -[53. Maximum Subarray (Easy)](https://leetcode.com/problems/maximum-subarray/description/) - -```html -For example, given the array [-2,1,-3,4,-1,2,1,-5,4], -the contiguous subarray [4,-1,2,1] has the largest sum = 6. -``` - -```java -public int maxSubArray(int[] nums) { - if (nums == null || nums.length == 0) { - return 0; - } - int preSum = nums[0]; - int maxSum = preSum; - for (int i = 1; i < nums.length; i++) { - preSum = preSum > 0 ? preSum + nums[i] : nums[i]; - maxSum = Math.max(maxSum, preSum); - } - return maxSum; -} -``` - **数组中等差递增子区间的个数** [413. Arithmetic Slices (Medium)](https://leetcode.com/problems/arithmetic-slices/description/)