Wins the Offer (Java version) Thirty-fifth question: given a list, returns its maximum continuous subsequence and

/ *
HZ occasionally get some professional issues to flicker those non-computer science students.
After the test group will finish today, he spoke up: in the old one-dimensional pattern recognition,
often need to calculate the maximum and continuous sub-vector when the vector is a positive whole number, the problem solved.
However, if the vector contains a negative number, it should contain a negative number, and expect a positive number next to it would make up for it?
For example: {6, -3, -2,7, -15,1,2,2}, and the maximum successive sub-vectors of 8 (beginning from 0, up to the third).
To an array and returns its maximum continuous subsequence and you will not be fooled him live? (Sub-vector length is at least 1)
* /

import java.util. *;

public class Class35 {

public int FindGreatestSumOfSubArray(int[] array){
int length = array.length;
if(array == null || length <= 0){
return 0;
}
ArrayList<Integer> auxiliary = new ArrayList<Integer>();
int count = array[0];
auxiliary.add(count);
if(length == 1){
return array[0];
}
for(int i = 1; i < length; i++){
count += array[i];
auxiliary.add(count);
}
return Collections.max(auxiliary);
}
public void test(){
int[] array = new int[]{6,-3,-2,7,-15,1,2,2};
System.out.println(FindGreatestSumOfSubArray(array));
}

public static void main(String[] args) {
// TODO Auto-generated method stub
Class35 c = new Class35();
c.test();

}

}

 

Guess you like

Origin www.cnblogs.com/zhuozige/p/12515945.html