[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 ...
随机推荐
- MySQL☞关联查询
关联查询:所需要的数据来源于多张表,通过表的连接查询(关联查询)来查询多张表中的数据 格式: select 别名1 . */列名 , 别名2 . */列名 from 表名1 别名1 , 表名2 别 ...
- python学习笔记03 --------------程序交互与格式化输出
1.读取用户输入内容 语法:input() 例: name = input('你的名字是?) print('你好'+name) 程序会等待用户输入名字后打印:你好(用户输入的名字) 注意:input接 ...
- Spring Cloud(六):Hystrix 监控数据聚合 Turbine【Finchley 版】
Spring Cloud(六):Hystrix 监控数据聚合 Turbine[Finchley 版] 发表于 2018-04-17 | 更新于 2018-05-07 | 上一篇我们介绍了使用 H ...
- 关于excle导数据的一些代码笔记
package com.bonc.util; import java.io.File; import java.io.FileInputStream; import java.io.FileOutpu ...
- [知识库:python-tornado]异步调用中的上下文控制Tornado stack context
异步调用中的上下文控制Tornado stack context https://www.zouyesheng.com/context-in-async-env.html 这篇文章真心不错, 非常透彻 ...
- 爬取CVPR 2018过程中遇到的坑
爬取 CVPR 2018 过程中遇到的坑 使用语言及模块 语言: Python 3.6.6 模块: re requests lxml bs4 过程 一开始都挺顺利的,先获取到所有文章的链接再逐个爬取获 ...
- [codecademy]html&css
1. HTML is the language used to create the web pages you visit everyday. It provides a logical way t ...
- Java之I/O流(第2部分)
1. 节点类类型: 2. 访问文件: Demo_1: import java.io.FileInputStream; import java.io.FileNotFoundException; imp ...
- 3dContactPointAnnotationTool开发日志(六)
一种可行的思路就是枚举一个模型的三角面片,然后判断三角形是否与另一个物体相交即可.为了让效果更好我想只渲染模型的线框. 在网上查了半天好像Unity里都没有自带的方便的渲染线框的方式,我又自己 ...
- 软工实践原型设计——PaperRepositories
软工实践原型设计--PaperRepositories 写在前面 本次作业链接 队友(031602237吴杰婷)博客链接 pdf文件地址 原型设计地址(加载有点慢...) 结对成员:031602237 ...