Find the contiguous subarray within an array (containing at least one number) which has the largest sum.
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.
public class Solution {
public int maxSubArray(int[] A) {
if(A.length==0) return 0;
if(A.length==1) return A[0];
int start=A[A.length-1];
int maxSum=A[A.length-1];
for(int i=A.length-2;i>=0;i--){
start=Math.max(A[i],start+A[i]);
maxSum=Math.max(maxSum,start);
}
return maxSum;
}
}思路:dp
本文介绍了一种算法来查找给定数组中和最大的连续子数组,并提供了实例演示和代码实现。
351

被折叠的 条评论
为什么被折叠?



