您的位置:首页 > 产品设计 > UI/UE

You’re given an array containing both positive and negative integers and required to find the sub-a

2012-08-22 19:01 447 查看
You’re given an array containing both positive and negative integers and required to find the sub-array with the largest sum













import java.util.ArrayList;

import java.util.List;



public class Duplicate {



public static int sum(List<Integer> wholeList,int start,int end){



int sum = 0;

for(int i=start;i<=end;i++)

{

sum=sum+wholeList.get(i);

}

return sum;

};





public static void main(String args[]){

int N=10;

List<Integer> wholeList=new ArrayList<Integer>();

for(int i=0;i<N;i++)

{

wholeList.add((int)(Math.random()*N)-N/2);

System.out.print(wholeList.get(i)+" ");

}







int maxSum=sum(wholeList,0,0);

int I = 0,J = 0;



for(int i=0;i<N;i++){

for(int j=i;j<N;j++){



if(sum(wholeList,i,j)>maxSum){maxSum=sum(wholeList,i,j);

I=i;

J=j;

}

}}



System.out.println();

System.out.print(maxSum +" "+I+" "+J);

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐