coding8832

public class MaxArray  {

    public static void main(String[] args) {
        Integer [] srcArray = {1,2,3,-8,9,10,6,-5,25};
        //记录数组下标
        int start=0, end = 0;
        //连续下标的和
        int sum=0;
        //最大值
        int max=0;

        Integer [] destArray=null;
        for(int i=0;i<srcArray.length;i++){
            for(int j=i+1;j<=srcArray.length;j++){
                destArray=new Integer[j-i];
                System.arraycopy(srcArray,i,destArray,0,j-i);
                sum=Arrays.asList(destArray).stream().mapToInt(value -> value).sum();
                if(sum>max){
                    max=sum;
                    start=i;
                    end=j;
                }
            }
        }
        Integer [] finallyArray=Arrays.copyOfRange(srcArray,start,end);
        System.out.println("数组下标start "+start+"->end "+(end-1));
        System.out.println("总和"+max);
        Arrays.asList(finallyArray).stream().forEach(integer -> System.out.print(integer.intValue() + "\n"));
    }
}

 

showtooltip

分类:

技术点:

相关文章:

  • 2021-06-03
  • 2021-10-17
  • 2022-02-17
  • 2021-10-17
  • 2022-02-20
  • 2022-03-01
  • 2021-09-30
猜你喜欢
  • 2022-12-23
  • 2021-12-21
  • 2022-12-23
  • 2022-01-27
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案