Maximum Subarray
【题目】
Given an integer array nums
, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum.
Example:
Input: [-2,1,-3,4,-1,2,1,-5,4],
Output: 6
Explanation: [4,-1,2,1] has the largest sum = 6.
Follow up:
If you have figured out the O(n) solution, try coding another solution using the divide and conquer approach, which is more subtle.
【分析】
这是一道非常简单的算法题,但是实现的方法却有很多种。在本篇文章中,博主想介绍三种巧妙的方法,这三种方法在面试和刷题过程中有非常广泛的应用。
方法一:Kadane算法
算法描述:
- 遍历该数组, 在遍历过程中, 将遍历到的元素依次累