算法-Maximum Subarray: a simpler solution

标签: leetcode array max-subary simulate
1205人阅读 评论(0) 收藏 举报
分类:

原题

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

查看评论

Lintcode42 Maximum Subarray II solution 题解

【题目描述】 Given an array of integers, find two non-overlapping subarrays which have the largest su...
  • xuxu_ban
  • xuxu_ban
  • 2017-05-05 16:19:28
  • 253

54.Maximum Subarray(动态规划)

Find the contiguous subarray within an array (containing at least one number) which has the largest ...
  • u010339647
  • u010339647
  • 2016-01-09 23:02:38
  • 250

最大和的子数组 Maximum subarray problem

最大和的子数组 Maximum subarray problem 给定一个数组,找到一个连续的子数组,使得这个子数组的和最大。 这里给出Kadane算法,复杂度是O(n)。 def max_...
  • jiyanfeng1
  • jiyanfeng1
  • 2013-02-06 14:13:42
  • 577

Find the maximum subarray XOR in a given array

geeksforgeeks的一篇文章: http://www.geeksforgeeks.org/find-the-maximum-subarray-xor-in-a-given-array/题目大...
  • u010232171
  • u010232171
  • 2015-11-06 17:13:22
  • 767

leedcode做题总结, 题目Maximum Subarray II & Maximum Subarray Difference

这道题是I的拓展,有点像stock的2但是还是挺不一样的,这里是求和不是求差,所以只需要对每个点i通过I的方法分别求出左右的最大子序列然后历遍相加即可,要注意的是left[i]储存的是0-i的最大su...
  • sdadssa1
  • sdadssa1
  • 2015-08-28 05:02:44
  • 380

LeetCode题解系列--718. Maximum Length of Repeated Subarray

描述Given two integer arrays A and B, return the maximum length of an subarray that appears in both ar...
  • bowen_wu_sysu
  • bowen_wu_sysu
  • 2017-11-01 21:29:40
  • 745

【leetcode】【53】Maximum Subarray

一、问题描述 Find the contiguous subarray within an array (containing at least one number) which has ...
  • xiaoliucool1314
  • xiaoliucool1314
  • 2016-03-01 11:21:30
  • 411

53. Maximum Subarray-动态规划/分治

题: Find the contiguous subarray within an array (containing at least one number) which has the la...
  • u011567017
  • u011567017
  • 2016-09-04 17:30:45
  • 320

[LeetCode] Maximum Product Subarray的4种解法

Find the contiguous subarray within an array (containing at least one number) which has the largest ...
  • bhwolf1987
  • bhwolf1987
  • 2014-09-26 12:11:40
  • 2527

#42 Maximum Subarray II

题目描述: Given an array of integers, find two non-overlapping subarrays which have the largest s...
  • haifischxia
  • haifischxia
  • 2016-08-29 03:11:50
  • 242
    算法channel

    交流思想,注重分析,实例阐述,通俗易懂,包含但不限于:经典算法,机器学习,深度学习,LeetCode 题解,Kaggle 实战。期待您的到来!

    算法与人工智能交流群:646901659

    个人资料
    专栏达人 持之以恒
    等级:
    访问量: 33万+
    积分: 7786
    排名: 3400
    博客专栏