最大连续子数组的和(记录子数组范围)

public class Main {
	
	public static void main(String[] args){
		Main m=new Main();
		int[] arr={1,-2,3,10,-4,-3,2,-5};
		maxSeq(arr);
	}
	public static int maxSeq(int[] arr){
		int n=arr.length;
		int sum=0;
		int max=Integer.MIN_VALUE;
		int start=0,end=0;
		for(int i=0;i<n;i++){
			if(sum>=0){
				//end++;
				sum=sum+arr[i];
			}else{
				start=i;
				end=i+1;
				sum=arr[i];
			}
			if(max<sum){
				end=i+1;
				max=sum;
			}
		}
		System.out.println(max);
		System.out.println("下标从"+start+"到"+(end-1));
		return max;
	}
}

posted @ 2017-10-12 21:31  cheerupWoo  阅读(119)  评论(0编辑  收藏  举报