[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-面试算法经典-Java实现】【152-Maximum Product Subarray(子数组的最大乘积)】

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

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版

(Java)LeetCode-53. Maximum Subarray

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

【Leetcode】Maximum Product Subarray JAVA实现

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

[LeetCode]Maximum Subarray

Write an efficient program to find the sum of contiguous subarray within a one-dimensional array of ...
  • jhbxlx
  • jhbxlx
  • 2014年09月25日 05:38
  • 454

LeetCode Maximum Product Subarray 解题报告

LeetCode 新题又更新了,最大子数组乘积 题目分析:求一个数组,连续子数组的最大乘积。...

LeetCode(53) Maximum Subarray

题目Find the contiguous subarray within an array (containing at least one number) which has the larges...
  • fly_yr
  • fly_yr
  • 2015年09月05日 20:35
  • 771

LeetCode—Maximum Product Subarray

Find the contiguous subarray within an array (containing at least one number) which has the largest ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:[LeetCode]Maximum Subarray(Java)
举报原因:
原因补充:

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