[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 ...
随机推荐
- vue设置title和ioc图标
vue设置ioc图标和title 1.ioc图标设置 在根目录中的index.html中引入代码: <link rel="shortcut icon" type=" ...
- 「小程序JAVA实战」小程序上传短视频(46)
转自:https://idig8.com/2018/09/14/xiaochengxujavashizhanxiaochengxushangchuanduanshipin45/ 个人信息:用户上传短视 ...
- oracle、sqlserver、mysql常用函数对比[to_char、to_number、to_date]
Oracle --> MySQL to_char(sysdat ...
- Unity 单例
1. 继承于MonoBehaviour(不随着场景切换而销毁) 基类代码: using System.Collections; using System.Collections.Generic; us ...
- 修改kvm虚拟机镜像大小
修改虚拟机镜像大小(qcow2/raw resize) 创建一个镜像文件,大小1G taw muxueqz@muxueqz /tmp $ qemu-img create -f raw t.raw 1G ...
- shell编程——sed用法之参数详解
sed格式: sed 参数 '正则表达式' 文件名 sed的常见参数有以下几种: 1.-n, --quiet, --silent 取消自动打印模式 不加n默认打印整个文件: [root@localho ...
- js闭包的定义
通过函数字面量创建的函数对象包含一个连接到外部上下文的连接,这叫做闭包. 还有一种定义:函数可以访问它被创建时所处的上下文环境,叫做闭包.
- Hadoop2.2.0安装配置手册
第一部分 Hadoop 2.2 下载 Hadoop我们从Apache官方网站直接下载最新版本Hadoop2.2.官方目前是提供了linux32位系统可执行文件,所以如果需要在64位系统上部署则需要单独 ...
- HRESULT:0x80070057 (E_INVALIDARG)
笔记本蓝屏后,在vs2010中调试项目时出现该异常, 解决方法:清空C:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Fi ...
- CloudFoundry 快速上手笔记
1.登陆cf 2.登陆进入webservice 3.查看ruby版本 4.查看gem版本 5.安装CF 6.配置cf Download the CLI from github: https://git ...