[LeetCode]Maximum Subarray(Java)

原创 2016年08月30日 20:33:39

典型的动态规划题,这道题我采用从后向前的思想找最大值

动态规划方程式就是代码的为代码

public class Solution {
    public int maxSubArray(int[] nums) {
        int[] addA = new int[nums.length];
        int max = 0,i = nums.length - 1,temp = 0;
        addA[i] = nums[i];
        max = addA[i];
        i--;
        while(i>=0){
            
            temp = addA[i+1] + nums[i];
            if(nums[i] > 0 && addA[i+1]>0){
                addA[i] = temp;
            }else if(nums[i]<= 0 && addA[i+1]>0 &&temp>0)
                addA[i] = temp;
            else 
                addA[i] = nums[i];
            if(addA[i] > max){
                max = addA[i];
            }    
            i--;
        }
        return max;
    }
}

2016/8/30

版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

【LeetCode-面试算法经典-Java实现】【053-Maximum Subarray(最大子数组和)】

【053-Maximum Subarray(最大子数组和)】【LeetCode-面试算法经典-Java实现】【所有题目目录索引】原题  Find the contiguous subarray wit...

【Leetcode】Maximum Product Subarray JAVA实现

题目描述: Find the contiguous subarray within an array (containing at least one number) which has the...

LeetCode 53. Maximum Subarray Java

53. Maximum Subarray   Find the contiguous subarray within an array (containing ...

leetcode解题之53 # Maximum Subarray Java版

leetcode解题之53 # Maximum Subarray Java版

【LeetCode-面试算法经典-Java实现】【152-Maximum Product Subarray(子数组的最大乘积)】

【152-Maximum Product Subarray(子数组的最大乘积)】【LeetCode-面试算法经典-Java实现】【所有题目目录索引】代码下载【https://github.com/Wa...

(Java)LeetCode-53. Maximum Subarray

Find the contiguous subarray within an array (containing at least one number) which has the largest ...

[LeetCode]Maximum Subarray

Write an efficient program to find the sum of contiguous subarray within a one-dimensional array of ...

LeetCode(53) Maximum Subarray

题目Find the contiguous subarray within an array (containing at least one number) which has the larges...

LeetCode—Maximum Product Subarray

Find the contiguous subarray within an array (containing at least one number) which has the largest ...

[leetcode]Maximum subarray

Maximum subarray是一个很常见的DP问题
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)