[Leetcode] maximun 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.
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)
{
if(n==) return ;
int sum=,maxValue=A[];
for(int i=;i<n;++i)
{
sum+=A[i];
maxValue=max(maxValue,sum);
if(sum<)
sum=;
}
return maxValue;
}
};
方法二:分治。参考Grandyang的博客。分治的思想类似二分搜索,首先将数组一分为二,分别找出左边和右边的最大子数组之和,然后从中间开始向左右分别扫描,求出的最大值分别和左右两边得到的最大值相比较,取最大值。
代码如下:
class Solution {
public:
int maxSubArray(int A[], int n)
{
if(n==) return ;
return helper(A,,n-);
} int helper(int A[],int left,int right)
{
if(left>=right) return A[left];
int mid=(left+right)>>;
int lmx=helper(A,left,mid-);
int rmx=helper(A,mid+,right); int mMax=A[mid],temp=mMax; //遍历左半端,
for(int i=mid-;i>=left;--i)
{
temp+=A[i];
mMax=max(mMax,temp);
}
temp=mMax; //向右
for(int i=mid+;i<=right;++i)
{
temp+=A[i];
mMax=max(mMax,temp);
} return max(mMax,max(lmx,rmx));
}
};
[Leetcode] maximun subarray 最大子数组的更多相关文章
- [LeetCode] Maximum Subarray 最大子数组
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- [LeetCode] 53. Maximum Subarray 最大子数组
Given an integer array nums, find the contiguous subarray (containing at least one number) which has ...
- [leetcode]53. Maximum Subarray最大子数组和
Given an integer array nums, find the contiguous subarray (containing at least one number) which has ...
- [LeetCode] 53. Maximum Subarray 最大子数组 --动态规划+分治
Given an integer array nums, find the contiguous subarray (containing at least one number) which has ...
- 【LeetCode每天一题】Maximum Subarray(最大子数组)
Given an integer array nums, find the contiguous subarray (containing at least one number) which has ...
- [LintCode] Maximum Subarray 最大子数组
Given an array of integers, find a contiguous subarray which has the largest sum. Notice The subarra ...
- Maximum Subarray(最大子数组)
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- [LeetCode] Maximum Size Subarray Sum Equals k 最大子数组之和为k
Given an array nums and a target value k, find the maximum length of a subarray that sums to k. If t ...
- [LeetCode] Maximum Product Subarray 求最大子数组乘积
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
随机推荐
- HBase 数据的多版本特性潜在的意外
HBase做为KeyValue结构存储,在存储上是依照RowKey的字典序进行排序,对于很多应用而言这可能远远不够,好在HBase的数据可以存储多个版本,并且版本可以排序,其理论上最大的版本数目Int ...
- 使用Docker部署java web项目
在国内可能会有源下载失败问题,docker安装失败, 这里提供docker离线安装包如有需要可以进行下载 docker离线安装包下载 ##本文环境使用centos 7 进行部署. #1安装docker ...
- Tunnel上传遇到字符[NUL]问题
模拟生产环境下数据格式,再现异常情景: Notepad++怎样输入字符[NUL]? 安装 Hex-Editor 插件: HexEditor插件用于在notepad++中查看16进制文件,只需要将此 ...
- Python3 Tkinter-OptionMenu
1.创建 from tkinter import * root=Tk() v=StringVar() v.set('xs') om=OptionMenu(root,v,'Python','PHP',' ...
- vs2008 c#项目调试dll源码,问题:“若要调试此模块,请将其项目生成配置更改为“调试”模式” 的解决方案
情况: 1:有程序 Trans.exe 的vs2008 c#源码:Trans.exe项目里引用了 Water.dll: 2:有Water.dll的项目源码: 3:想在Trans.exe里调试Water ...
- Unicode,UTF-32,UTF-16,UTF-8到底是啥关系?
编码的目的,就是给抽象的字符赋予一个数值,好在计算机里面表示.常见的ASCII使用8bit给字符编码,但是实际只使用了7bit,最高位没有使用,因此,只能表示128个字符:ISO-8859-1(也叫L ...
- 自测之Lesson4:gdb
题目:列出gdb过程中常用的命令. 常用命令: 命令 作用 使用示例1 使用示例2 list 列出代码 list 行号 list 函数名 break 设置断点 break 行号 b 行号 run 运行 ...
- “Hello world!”贡献分分配规则
规则1:得到总分后取14分,剩下分数进行平分.(备注:例如得了50分,取出14分,剩下36分组内进行平分) 规则2:对于会议准时参加者每人加0.5分.(备注:按通知开会时间为准,准时到者实行加分.) ...
- Notes of the scrum meeting before publishing2(12.18)
meeting time:18:30~20:30p.m.,December 18th,2013 meeting place:3号公寓一层 attendees: 顾育豪 ...
- java设计模式简介
设计模式简介: 设计模式(Design pattern)代表了最佳的实践,通常被有经验的面向对象的软件开发人员所采用.设计模式是软件开发人员在软件开发过程中面临的一般问题的解决方案.这些解决方案是众多 ...