Java: seeking a continuous array of sub-elements and maximum


{class TestArray public
public static int FindGreatestSumOfSubArray (int [] Array) {
IF (== 0 || be array.length Array == null) {
return 0;
}
int currentSum = 0; // store the current item and continuous n
int max = array [0]; // initializes a first maximum number of successive sub-elements and
for (int I = 0; I <be array.length; I ++) {
// the core part, well understood.
IF (currentSum <= 0 ) {// the current through the continuous n items and less than or equal to 0, it is not necessary with the addition elements behind
currentSum = array [i]; // reassigned currentSum
} the else {
currentSum + = Array [I]; // If the value is greater than 0 currentSum, continue adding and rear elements,
}
IF (currentSum> max) {// each value currentSum has changed compared with the max
max = currentSum; // if the value is greater than max currentSum , then a value is assigned to currentSum max
}
}
return max;
}
public static void main (String [] args) {
int [] = {Array. 6, -3,-2,7, -15,1,2,2};
// int [] Array = {-6, -3, -2, -7, -15, - . 1, -2, -2};
int Result = FindGreatestSumOfSubArray (Array);
System.out.println ( "successive sub-element and the maximum is:" Result +);
}
}
successive sub-element and the maximum is: 8

---------------------

Guess you like

Origin www.cnblogs.com/hyhy904/p/11257587.html