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.

More practice:

If you have figured out the O(n) solution, try coding another solution using the divide and conquer approach, which is more subtle.

 
在累加的过程中,如果发现sum<0则说明前面的序列对后面的序列没有贡献,故此时设置sum=0
 
  1. class Solution {
  2. public:
  3. int maxSubArray(int A[], int n) {
  4.  
  5. int sum,maxSum;
  6. sum=maxSum=A[];
  7. for(int i=;i<n;i++)
  8. {
  9. if(sum<) sum=;
  10. sum+=A[i];
  11.  
  12. if(maxSum<sum) maxSum=sum;
  13. }
  14. return maxSum;
  15. }
  16. };
采用分治法求解:
找到左半边最大的序列值,找到右半边最大的序列值,找到中间序列的值
 
  1. class Solution {
  2. public:
  3. int maxSubArray(int A[], int n) {
  4.  
  5. divideAndConquer(A,,n-);
  6. }
  7.  
  8. int divideAndConquer(int A[],int left,int right)
  9. {
  10.  
  11. if(left==right) return A[left];
  12.  
  13. int mid=(left+right)/;
  14.  
  15. int leftMax=divideAndConquer(A,left,mid);
  16. int rightMax=divideAndConquer(A,mid+,right);
  17.  
  18. int midSum1=;
  19. int midMax1=A[mid];
  20.  
  21. for(int i=mid;i>=left;i--)
  22. {
  23. midSum1+=A[i];
  24. if(midMax1<midSum1) midMax1=midSum1;
  25. }
  26.  
  27. int midSum2=;
  28. int midMax2=A[mid+];
  29.  
  30. for(int i=mid+;i<=right;i++)
  31. {
  32. midSum2+=A[i];
  33. if(midMax2<midSum2) midMax2=midSum2;
  34. }
  35.  
  36. int midMax=midMax1+midMax2;
  37.  
  38. return max(max(leftMax,rightMax),midMax);
  39.  
  40. }
  41. };

【leetcode】Maximum Subarray的更多相关文章

  1. 【leetcode】Maximum Subarray (53)

    1.   Maximum Subarray (#53) Find the contiguous subarray within an array (containing at least one nu ...

  2. 【LeetCode】Maximum Subarray(最大子序和)

    这道题是LeetCode里的第53道题. 题目描述: 给定一个整数数组 nums ,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和. 示例: 输入: [-2,1,-3,4,-1 ...

  3. 【Leetcode】【Medium】Maximum Subarray

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

  4. 【LeetCode】Maximum Product Subarray 求连续子数组使其乘积最大

    Add Date 2014-09-23 Maximum Product Subarray Find the contiguous subarray within an array (containin ...

  5. 【Leetcode】Maximum Product Subarray

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

  6. 【数组】Maximum Subarray

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

  7. 【LeetCode】713. Subarray Product Less Than K 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/subarray ...

  8. 【leetcode】Maximum Gap

    Maximum Gap Given an unsorted array, find the maximum difference between the successive elements in ...

  9. 【leetcode】Maximum Gap(hard)★

    Given an unsorted array, find the maximum difference between the successive elements in its sorted f ...

随机推荐

  1. 深入理解Java:注解(Annotation)--注解处理器

    如果没有用来读取注解的方法和工作,那么注解也就不会比注释更有用处了.使用注解的过程中,很重要的一部分就是创建于使用注解处理器.Java SE5扩展了反射机制的API,以帮助程序员快速的构造自定义注解处 ...

  2. asp.net mvc中在使用async的时候HttpContext为null的问题

    摘要 HttpContext上下文并不是无处不在的.详情可以看下Fish Li的文章,解释的比较清楚. HttpContext.Current并非无处不在 问题复现 public async Task ...

  3. 如何才能实现在点击链接时直接在网页中打开word文档,但不提示保存

    一般要直接打开需要客户端 1.客户端有word支持 2.客户端浏览器的版本与设置 可寻找一下相关的控件或中间件,我的意见是看能否变通一下,把word转成HTML或PDF再展示给用户.(若用户不需要编辑 ...

  4. 如何在html结构标签中使用js 变量 生成可变化的 title标题?

    如果form的action不写, 或 action="", 那么就表示 将数据发送到 本文件 当前文件自身... 1. 在jquery的选择器中, $()括号中的内容是一个 exp ...

  5. linux程序设计1

    a.out 的意思是 assembler output,即汇编输出. C语言的头文件一般位于 /usr/include/ 目录下,而依赖于特定 Linux 版本的头文件通常可在目录 /usr/incl ...

  6. ApacheServer-----关于443端口被占用的解决方法

    最经公司项目需要经过Apache服务器转发,自己也下载了ApacheServer,但是在启动的过程中,遇到443端口被占用,网上看了一些解决方法,都不对,没有解决问题. 执行启动命令httpd -k ...

  7. iOS 字符转换

    字典或者数组转化为nsstring NSArray *arr = [NSArray arrayWithObject:@"1"]; NSString *str = [arr JSON ...

  8. 基于iSCSI的SQL Server 2012群集测试(三)--SQL Server 2012群集安装总结

    5.SQL Server 2012群集安装总结 5.1 群集与非群集的安装区别总结 SQL Server虚拟名称: 非群集环境下,本地服务器的名称就是SQL Server服务器名称:但在群集环境下,由 ...

  9. ASP.NET原理分析

    ASP.NET请求与处理全过程分析 1.用户向服务器的某IP端口发送请求,此端口通过Http.sys来管理,请求报文被Http.sys接收,Http.sys在注册表中找能处理这个请求类型的应用程序,最 ...

  10. wordpress不用插件实现Pagenavi页面导航功能

    Pagenavi 是一个很好的功能,现在 WordPress 博客一般都是使用 WP-Pagenavi 插件来实现,但是如果插件一多的话整个wordpress效率就降低了,我们力求用尽量少的插件来实现 ...