[LeetCode 题解]: Maximum Subarray
前言
【LeetCode 题解】系列传送门: http://www.cnblogs.com/double-win/category/573499.html
1.题目描述
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.
2. 解法
- 1 class Solution {
- 2 public:
- 3 int maxSubArray(int A[], int n) {
- 4 int sum,ans,i,mark=0;
- 5 sum=ans=i=0;
- 6
- 7 for(i=0;i<n;i++)
- 8 if(A[i]>=0) mark=1;
- 9
- 10 if(mark==0)
- 11 {
- 12 ans=A[0];
- 13 for(i=1;i<n;i++)
- 14 if(A[i]>ans) ans=A[i];
- 15
- 16 }
- 17 else
- 18 {
- 19 for(i=0;i<n;i++)
- 20 {
- 21 sum+=A[i];
- 22 if(sum<0)
- 23 sum=0;
- 24 if(sum>ans) ans=sum;
- 25 }
- 26 }
- 27 return ans;
- 28 }
- 29 };
3. 相关题目
Gas Station 题解 http://www.cnblogs.com/double-win/p/3746637.html
![]() |
作者:Double_Win 出处: http://www.cnblogs.com/double-win/p/3746672.html 声明: 由于本人水平有限,文章在表述和代码方面如有不妥之处,欢迎批评指正~ |
[LeetCode 题解]: Maximum Subarray的更多相关文章
- [array] leetcode - 53. Maximum Subarray - Easy
leetcode - 53. Maximum Subarray - Easy descrition Find the contiguous subarray within an array (cont ...
- 小旭讲解 LeetCode 53. Maximum Subarray 动态规划 分治策略
原题 Given an integer array nums, find the contiguous subarray (containing at least one number) which ...
- LeetCode 53. Maximum Subarray(最大的子数组)
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- 【leetcode】Maximum Subarray (53)
1. Maximum Subarray (#53) Find the contiguous subarray within an array (containing at least one nu ...
- 【leetcode】Maximum Subarray
Maximum Subarray Find the contiguous subarray within an array (containing at least one number) which ...
- leetCode 53.Maximum Subarray (子数组的最大和) 解题思路方法
Maximum Subarray Find the contiguous subarray within an array (containing at least one number) whic ...
- 41. leetcode 53. Maximum Subarray
53. Maximum Subarray Find the contiguous subarray within an array (containing at least one number) w ...
- Leetcode#53.Maximum Subarray(最大子序和)
题目描述 给定一个序列(至少含有 1 个数),从该序列中寻找一个连续的子序列,使得子序列的和最大. 例如,给定序列 [-2,1,-3,4,-1,2,1,-5,4], 连续子序列 [4,-1,2,1] ...
- LN : leetcode 53 Maximum Subarray
lc 53 Maximum Subarray 53 Maximum Subarray Find the contiguous subarray within an array (containing ...
随机推荐
- 本地YUM仓库搭建实战
YUM主要用于自动安装.升级rpm软件包,它能自动查找并解决rpm包之间的依赖关系.要成功的使用YUM工具安装更新软件或系统,就需要有一个包含各种rpm软件包的repository(软件仓库),这个软 ...
- bigdata
1.打开cygwin,启动hadoop,运行jps命令查看节点启动情况 2.切换到hadoop根目录,运行指令 echo "hello boy hei baby hello word hel ...
- 读《分布式一致性原理》CURATOR客户端3
分布式锁 在分布式环境中,为了保证数据的一致性,经常在程序运行的某个运行点.需要进行同步控制. package master; import java.text.SimpleDateFormat; i ...
- C# 判断字体是否存在以及安装
1. 字体安装 在实际开发项目中,需要在客户端安装字体,一种是通过代码将字体文件复制到系统FONT目录即可,另一种通过安装文件实现,至于其他方式还未知晓. 1.1 软安装 public cla ...
- delphi IOS 后台状态保存
FormSaveState procedure TFrm.FormSaveState(Sender: TObject);begin end; http://stackoverflow.com/ques ...
- flutter container image FittedBox AspectRatio
当container指定了大小时,里面放入图片后,图片是居中自适应的,根据图片的大小,垂直居中或者水平居中.因为Image的默认自适应就是Contain, BoxFit.Contain 如果conta ...
- vector(实现存图)
#include<cstdio> #include<algorithm> #include<cstring> #include<iostream> #i ...
- selenium安装方式
selenium的二种安装方式 1.在线安装:打开cmd输入, pip.ext install selenium 2.离线安装,下载selenium安装包,然后解压,在cmd中进入到解压的文件中,在运 ...
- NormalMapping
[NormalMapping] 法线贴图内的数据是法线,高度贴图内的数据是高度,不是一个东西.在ShaderLab中,UnpackNormal()分析的是法线贴图(注意不是高度贴图). 可以看到,在G ...
- 【LA2957 训练指南】运送超级计算机【二分,最大流】
题意: 宇宙中有n个星球,你的任务是用最短的时间把k个超级计算机从星球S运送到星球T.每个超级计算机需要一整艘飞船来运输.行星之间有m条双向隧道,每条隧道需要一整天的时间来通过,且不能有两艘飞船同时使 ...