First, title:
An array of integers with a positive negative integer, in which one or more integers constitute an integer group, with one and, for the maximum subarray, required, time complexity O (n)
Second, design ideas:
1, first enter the number of elements to be entered.
2, build two int type Word group, one to hold the number, one as a temporary array.
3, meet a positive number is stored in the temporary array, and continue to add, as long as the result is just continue to store. Until a negative number is met, truncation is processed.
4. Sort the number in the temporary array (without bubbling, taking into account O (n)), output.
Three. Source
PackageShuzuadd;ImportJava.util.Scanner; Public classMain { Public Static voidMain (string[] args) {//TODO Auto-generated method stubsScanner in =NewScanner (system.in); intN//Shuruchangdun=In.nextint (); intarray1[]=New int[n]; intarray2[]=New int[N]; intSum,geshu=0; intMax = 0; for(inti=0;i<n;i++) {Array1[i]=In.nextint (); } Sum=0; for(ints=0;s<n;s++) { if(sum<0) {sum=array1[s+1]; } if(sum>=0) {sum=sum+Array1[s]; Array2[geshu]=sum; Geshu++; } } for(intx=0;x<=geshu;x++) { if(array2[x]<array2[x+1]) Max=array2[x+1]; if(array2[x]>=array2[x+1]) array2[x+1]=Array2[x]; } System.out.println (max); }}
Four. Run:
Classroom test for maximum word count and