LeetCode-Easy刷题(11) Maximum Subarray

Posted 当以乐

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LeetCode-Easy刷题(11) Maximum Subarray相关的知识,希望对你有一定的参考价值。

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

For example, given the array [-2,1,-3,4,-1,2,1,-5,4],
the contiguous subarray [4,-1,2,1] has the largest sum = 6.


求连续子串和的最大值.


 //动态规划
    public static int maxSubArray(int[] nums) 
        if(nums==null || nums.length<1)
            return 0;
        
        int local = nums[0];
        int global = nums[0];
        for (int i = 1; i < nums.length; i++) 
             local = Math.max(nums[i], local + nums[i]);//局部最优解
             global = Math.max(local, global);//全局最优解
        
        return global;
    


以上是关于LeetCode-Easy刷题(11) Maximum Subarray的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode-Easy刷题(14) Add Binary

LeetCode-Easy刷题 Valid Parentheses

LeetCode-Easy刷题(31) Single Number

LeetCode-Easy刷题 Remove Element

LeetCode-Easy刷题(33) Min Stack

LeetCode-Easy刷题(33) Min Stack