当前位置:七道奇文章资讯编程技术Java编程
日期:2011-03-22 16:13:00  来源:本站整理

Java求数组中持续n个元素使其和最大[Java编程]

赞助商链接



  本文“Java求数组中持续n个元素使其和最大[Java编程]”是由七道奇为您精心收集,来源于网络转载,文章版权归文章作者所有,本站不对其观点以及内容做任何评价,请读者自行判断,以下是其具体内容:

给定一个数组,求出数组中持续的一些元素使其和的值最大.假如全部元素都为正数,明显整个数组即为所求的.假如全部元素的值为负数,则所求的最大值为0.

这是在编程珠玑上看到的,当时间复杂度由O(n3)减为O(n)了.

java代码

package cn.lifx.test;

public class MaxSum
{
  public static void main(String[] args)
  {
  int[] arr = new int[]{31, -41, 59, 26, -53, 58, 97, -93, -23, 84};

  MaxSum ms = new MaxSum();

  ms.Max(arr);
  ms.Max2(arr);
  ms.Max3(arr);

  int max = ms.Max4(arr, 0, arr.length-1);
  System.out.println("Max sum is " + max);

  ms.Max5(arr);
  }

  //办法1: 时间复杂度为O(n*n*n)
  public void Max(int[] arr)
  {
  int max = 0;
  int sum = 0;
  int left = -1;
  int right = -1;

  for(int i=0; i<arr.length; i++)
  {
   for(int j=i; j<arr.length; j++)
   {
   sum = 0;

   for(int k=i; k<=j; k++)
   {
    sum = sum + arr[k];
   }

   if(sum > max)
   {
    left = i;
    right = j;
    max = sum;
   }
   }
  }

  if(right > 0)
  {
   System.out.println("Max is from element " + left + "(" + arr[left] + ") to element " + right + "(" + arr[right] + "), max sum is " + max);
  }
  else
  {
   System.out.println("Max sum is 0 .");
  }
  }

  //办法2:时间复杂度为O(n*n)
  public void Max2(int[] arr)
  {
  int max = 0;
  int sum = 0;
  int left = -1;
  int right = -1;

  for(int i=0; i<arr.length; i++)
  {
   sum = 0;
   for(int j=i; j<arr.length; j++)
   {
   sum = sum + arr[j];
   if(sum > max)
   {
    left = i;
    right = j;
    max = sum;
   }
   }
  }

  if(right > 0)
  {
   System.out.println("Max is from element " + left + "(" + arr[left] + ") to element " + right + "(" + arr[right] + "), max sum is " + max);
  }
  else
  {
   System.out.println("Max sum is 0 .");
  }
  }

  //办法3:时间复杂度为O(n*n)
  public void Max3(int[] arr)
  {
  int max = 0;
  int sum = 0;
  int left = -1;
  int right = -1;

  int[] temp = new int[arr.length+1];

  temp[0] = 0;
  for(int i=0; i<arr.length; i++)
  {
   temp[i+1] = temp[i] + arr[i];
  }

  for(int i=0; i<arr.length; i++)
  {
   for(int j=i; j<temp.length; j++)
   {
   sum = temp[j] - temp[i];
   if(sum > max)
   {
    left = i;
    right = j-1;
    max = sum;
   }
   }
  }

  if(right > 0)
  {
   System.out.println("Max is from element " + left + "(" + arr[left] + ") to element " + right + "(" + arr[right] + "), max sum is " + max);
  }
  else
  {
   System.out.println("Max sum is 0 .");
  }
  }

  //办法4:时间复杂度为O(n*logn)
  public int Max4(int[] arr, int left, int right)
  {
  int sum = 0;
  int max = 0;
  int max1 = 0;
  int max2 = 0;
  int middle = 0;

  if(left > right)
  {
   return 0;
  }
  else if(left == right)
  {
   return (arr[left] > 0 ? arr[left] : 0);
  }

  middle = (left + right)/2;

  for(int i=middle; i>=left; i--)
  {
   sum = sum + arr[i];
   if(sum > max1)
   {
   max1 = sum;
   }
  }

  sum=0;
  for(int i=middle+1; i<=right; i++)
  {
   sum = sum + arr[i];
   if(sum > max2)
   {
   max2 = sum;
   }
  }

  max = max1+max2;
  int temp1 = Max4(arr, left, middle);
  int temp2 = Max4(arr, middle+1, right);

  if(temp1 > max)
  {
   max = temp1;
  }

  if(temp2 > max)
  {
   max = temp2;
  }

  return max;
  }

  //办法5:时间复杂度为O(n)
  public void Max5(int[] arr)
  {
  int max1 = 0;
  int max2 = 0;
  int left = -1;
  int right = -1;
  int temp = 0;

  for(int i=0; i<arr.length; i++)
  {
   temp = (max1+arr[i]);
   if(temp > 0)
   {
   max1 = temp;
   }
   else
   {
   left = i+1;
   max1 = 0;
   }

   if(max1 > max2)
   {
   right = i;
   max2 = max1;
   }
  }

  if(right > 0)
  {
   System.out.println("Max is from element " + left + "(" + arr[left] + ") to element " + right + "(" + arr[right] + "), max sum is " + max2);
  }
  else
  {
   System.out.println("Max sum is 0 .");
  }
  }
}

输出为:

Java代码

Max is from element 2(59) to element 6(97), max sum is 187
Max is from element 2(59) to element 6(97), max sum is 187
Max is from element 2(59) to element 6(97), max sum is 187
Max sum is 187
Max is from element 2(59) to element 6(97), max sum is 187


  以上是“Java求数组中持续n个元素使其和最大[Java编程]”的内容,如果你对以上该文章内容感兴趣,你可以看看七道奇为您推荐以下文章:
  • 利用Javascript实现网页水印(非图片水印)
  • Java开辟环境的搭建
  • Ubuntu java安装与配置
  • 办理Ubuntu 10.04 Firefox3.6 Java浏览器插件不工作的问
  • Ubuntu重装后Java环境的设置
  • Sun Java进入Ubuntu 10.10软件中央
  • Ubuntu 10.10配置Java开辟环境
  • 在Ubuntu 10.10中配置Java环境变量的办法
  • Ubuntu下Java环境的搭建
  • Ubuntu 10.04 下安装 Java, JRE
  • Ubuntu 10.04下的搭建SUN JAVA开辟环境
  • Ubuntu 12.04安装java7
  • 本文地址: 与您的QQ/BBS好友分享!
    • 好的评价 如果您觉得此文章好,就请您
        0%(0)
    • 差的评价 如果您觉得此文章差,就请您
        0%(0)

    文章评论评论内容只代表网友观点,与本站立场无关!

       评论摘要(共 0 条,得分 0 分,平均 0 分) 查看完整评论
    Copyright © 2020-2022 www.xiamiku.com. All Rights Reserved .