经典问题:

给定一个int[]数组,求其最大子序列之和(条件:数组中不全部都是负数)。

最优算法,线性时间复杂度:

public static int maxSubSum(int[] a){
    int maxSum = 0;
    int thisSum = 0;
    for(int i=0; i<a.length; i++){
        thisSum += a[i];
        if(thisSum > maxSum)
            maxSum = thisSum;
        else if(thisSum<0)
             thisSum = 0;
    }  
     return maxSum;
}

 

 

 

相关文章:

  • 2021-08-08
  • 2022-02-01
  • 2021-10-23
  • 2021-10-15
  • 2021-04-26
  • 2022-12-23
  • 2022-02-03
  • 2021-12-26
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-11-17
  • 2022-03-04
  • 2022-03-05
  • 2022-12-23
  • 2021-07-26
相关资源
相似解决方案