算法-Maximum Subarray: a simpler solution

原创 2017年04月29日 17:55:13

原题

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 int MaxSubArray(int[] nums) 
    {
           int premax =nums[0];
           int curmax = nums[0];
           for(int i=1;i<nums.Length;i++)
           {
               curmax = Math.Max(nums[i], nums[i] + curmax);
               premax = Math.Max(curmax, premax);
           }
           return premax;
    }

模拟

这里写图片描述

leetcode测试结果

这里写图片描述

Array
http://download.csdn.net/detail/daigualu/9829402
DP
http://download.csdn.net/detail/daigualu/9829404
Tree
http://download.csdn.net/detail/daigualu/9829406
Stack
http://download.csdn.net/detail/daigualu/9829405
及其他

leetcode-solution库

leetcode算法题目解决方案每天更新在github库中,欢迎感兴趣的朋友加入进来,也欢迎star,或pull request。https://github.com/jackzhenguo/leetcode-csharp

版权声明:本文为博主原创文章,欢迎转载,请注明 http://blog.csdn.net/daigualu

Lintcode42 Maximum Subarray II solution 题解

【题目描述】 Given an array of integers, find two non-overlapping subarrays which have the largest su...

【LeetCode】【Solution】Maximum Product Subarray

【题目】 Find the contiguous subarray within an array (containing at least one number) which has th...
  • ljiabin
  • ljiabin
  • 2014年11月11日 17:00
  • 1464

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

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

【算法导论学习-007】最大子数组和问题(Maximum subarray problem)

参考:算法导论P68      这是个经典问题。

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

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

算法643. Maximum Average Subarray I

Given an array consisting of n integers, find the contiguous subarray of given length k that has the...

分治算法解题:Maximum Subarray

leetcode上的分治算法有这样一道题: Find the contiguous subarray within an array (containing at least one numbe...

最大子数组问题 Maximum Subarray

Maximum Subarray: algorithm这个问题我们先看下问题的描述:问题描述Find the contiguous subarray within an array (containi...

leetCode 53.Maximum Subarray (子数组的最大和) 解题思路方法

Maximum Subarray  Find the contiguous subarray within an array (containing at least one number) w...

Maximum Subarray 最大子数组和(连续)@LeetCode

package Level3; import java.util.Arrays; /** * * Maximum Subarray * * Find the contiguous sub...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:算法-Maximum Subarray: a simpler solution
举报原因:
原因补充:

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