在数组中寻找连续子数组和最大值

来源:互联网 发布:孙鑫java视频教程 编辑:程序博客网 时间:2024/05/21 21:44

算法分析:

定义tempArray存放子数组的和,当tempArray[i-1]小于0时,把tempArray[i] = array[i],否则tempArray[i] = tempArray[i - 1] + array[i]。

public class findMaxContinueSubArray {

    public int findMaxContinueSubArray(int [] array){
        int[] tempArray = new int[array.length];
        tempArray[0] = array[0];
        int max = array[0];

        for(int i = 1;i <= array.length;++i){
            if(tempArray[i-1] <= 0){
                tempArray[i] = array[i];
            }else{
                tempArray[i] = tempArray[i - 1] + array[i];
            }
            if(tempArray[i] > max){
                max = tempArray[i];
            }
        }
    }
}
原创粉丝点击