【leetcode】Maximum Subarray
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.
- class Solution {
- public:
- int maxSubArray(int A[], int n) {
- int sum,maxSum;
- sum=maxSum=A[];
- for(int i=;i<n;i++)
- {
- if(sum<) sum=;
- sum+=A[i];
- if(maxSum<sum) maxSum=sum;
- }
- return maxSum;
- }
- };
- class Solution {
- public:
- int maxSubArray(int A[], int n) {
- divideAndConquer(A,,n-);
- }
- int divideAndConquer(int A[],int left,int right)
- {
- if(left==right) return A[left];
- int mid=(left+right)/;
- int leftMax=divideAndConquer(A,left,mid);
- int rightMax=divideAndConquer(A,mid+,right);
- int midSum1=;
- int midMax1=A[mid];
- for(int i=mid;i>=left;i--)
- {
- midSum1+=A[i];
- if(midMax1<midSum1) midMax1=midSum1;
- }
- int midSum2=;
- int midMax2=A[mid+];
- for(int i=mid+;i<=right;i++)
- {
- midSum2+=A[i];
- if(midMax2<midSum2) midMax2=midSum2;
- }
- int midMax=midMax1+midMax2;
- return max(max(leftMax,rightMax),midMax);
- }
- };
【leetcode】Maximum Subarray的更多相关文章
- 【leetcode】Maximum Subarray (53)
1. Maximum Subarray (#53) Find the contiguous subarray within an array (containing at least one nu ...
- 【LeetCode】Maximum Subarray(最大子序和)
这道题是LeetCode里的第53道题. 题目描述: 给定一个整数数组 nums ,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和. 示例: 输入: [-2,1,-3,4,-1 ...
- 【Leetcode】【Medium】Maximum Subarray
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- 【LeetCode】Maximum Product Subarray 求连续子数组使其乘积最大
Add Date 2014-09-23 Maximum Product Subarray Find the contiguous subarray within an array (containin ...
- 【Leetcode】Maximum Product Subarray
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- 【数组】Maximum Subarray
题目: Find the contiguous subarray within an array (containing at least one number) which has the larg ...
- 【LeetCode】713. Subarray Product Less Than K 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/subarray ...
- 【leetcode】Maximum Gap
Maximum Gap Given an unsorted array, find the maximum difference between the successive elements in ...
- 【leetcode】Maximum Gap(hard)★
Given an unsorted array, find the maximum difference between the successive elements in its sorted f ...
随机推荐
- 深入理解Java:注解(Annotation)--注解处理器
如果没有用来读取注解的方法和工作,那么注解也就不会比注释更有用处了.使用注解的过程中,很重要的一部分就是创建于使用注解处理器.Java SE5扩展了反射机制的API,以帮助程序员快速的构造自定义注解处 ...
- asp.net mvc中在使用async的时候HttpContext为null的问题
摘要 HttpContext上下文并不是无处不在的.详情可以看下Fish Li的文章,解释的比较清楚. HttpContext.Current并非无处不在 问题复现 public async Task ...
- 如何才能实现在点击链接时直接在网页中打开word文档,但不提示保存
一般要直接打开需要客户端 1.客户端有word支持 2.客户端浏览器的版本与设置 可寻找一下相关的控件或中间件,我的意见是看能否变通一下,把word转成HTML或PDF再展示给用户.(若用户不需要编辑 ...
- 如何在html结构标签中使用js 变量 生成可变化的 title标题?
如果form的action不写, 或 action="", 那么就表示 将数据发送到 本文件 当前文件自身... 1. 在jquery的选择器中, $()括号中的内容是一个 exp ...
- linux程序设计1
a.out 的意思是 assembler output,即汇编输出. C语言的头文件一般位于 /usr/include/ 目录下,而依赖于特定 Linux 版本的头文件通常可在目录 /usr/incl ...
- ApacheServer-----关于443端口被占用的解决方法
最经公司项目需要经过Apache服务器转发,自己也下载了ApacheServer,但是在启动的过程中,遇到443端口被占用,网上看了一些解决方法,都不对,没有解决问题. 执行启动命令httpd -k ...
- iOS 字符转换
字典或者数组转化为nsstring NSArray *arr = [NSArray arrayWithObject:@"1"]; NSString *str = [arr JSON ...
- 基于iSCSI的SQL Server 2012群集测试(三)--SQL Server 2012群集安装总结
5.SQL Server 2012群集安装总结 5.1 群集与非群集的安装区别总结 SQL Server虚拟名称: 非群集环境下,本地服务器的名称就是SQL Server服务器名称:但在群集环境下,由 ...
- ASP.NET原理分析
ASP.NET请求与处理全过程分析 1.用户向服务器的某IP端口发送请求,此端口通过Http.sys来管理,请求报文被Http.sys接收,Http.sys在注册表中找能处理这个请求类型的应用程序,最 ...
- wordpress不用插件实现Pagenavi页面导航功能
Pagenavi 是一个很好的功能,现在 WordPress 博客一般都是使用 WP-Pagenavi 插件来实现,但是如果插件一多的话整个wordpress效率就降低了,我们力求用尽量少的插件来实现 ...