前言

 

【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.

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.

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的更多相关文章

  1. [array] leetcode - 53. Maximum Subarray - Easy

    leetcode - 53. Maximum Subarray - Easy descrition Find the contiguous subarray within an array (cont ...

  2. 小旭讲解 LeetCode 53. Maximum Subarray 动态规划 分治策略

    原题 Given an integer array nums, find the contiguous subarray (containing at least one number) which ...

  3. LeetCode 53. Maximum Subarray(最大的子数组)

    Find the contiguous subarray within an array (containing at least one number) which has the largest ...

  4. 【leetcode】Maximum Subarray (53)

    1.   Maximum Subarray (#53) Find the contiguous subarray within an array (containing at least one nu ...

  5. 【leetcode】Maximum Subarray

    Maximum Subarray Find the contiguous subarray within an array (containing at least one number) which ...

  6. leetCode 53.Maximum Subarray (子数组的最大和) 解题思路方法

    Maximum Subarray  Find the contiguous subarray within an array (containing at least one number) whic ...

  7. 41. leetcode 53. Maximum Subarray

    53. Maximum Subarray Find the contiguous subarray within an array (containing at least one number) w ...

  8. Leetcode#53.Maximum Subarray(最大子序和)

    题目描述 给定一个序列(至少含有 1 个数),从该序列中寻找一个连续的子序列,使得子序列的和最大. 例如,给定序列 [-2,1,-3,4,-1,2,1,-5,4], 连续子序列 [4,-1,2,1] ...

  9. LN : leetcode 53 Maximum Subarray

    lc 53 Maximum Subarray 53 Maximum Subarray Find the contiguous subarray within an array (containing ...

随机推荐

  1. cento7.3下玩转sphinx

    cento7.5下玩转sphinx 1 安装依赖文件 yum install postgresql-libs unixODBC 2 下载 wget http://sphinxsearch.com/fi ...

  2. 转 CentOS下面安装RVM+ruby+Rails

    CentOS6.2下面安装RVM+ruby+Rails (1)RVM官方网站应该是改版过一次, 使用 curl -L https://get.rvm.io | bash -s stable 下载并安装 ...

  3. Git(二):常用 Git 命令清单

    转: http://www.ruanyifeng.com/blog/2015/12/git-cheat-sheet.html 我每天使用 Git ,但是很多命令记不住. 一般来说,日常使用只要记住下图 ...

  4. Linux安装MariaDB+初始化数据库

    背景说明: 在数据库中,mysql的是常用的数据库之一:作为一款开源的软件被广大公司所使用. 但是,mysql在被Oracle公司收购后,难免在以后会有取消开源的问题.所以急需一款新的数据库产品替换m ...

  5. Java安全框架 Apache Shiro学习-1-ini 配置

    简单登录流程: 1.  SecurityManager   2.  SecurityUtils.setSecurityManager 3.  SecurityUtils.getSubject     ...

  6. 在MyEclipse中用debug调试应用程序

    F5:单步测试,作用是跳入,比如说一大步中分为10小步,单击F5一次就会走完一小步,走完这一大步则需要单步10次.F6:与F5一样也是单步测试.只不过与F5不同的是F5追求的是过程,而F6追求的是结果 ...

  7. flask 常见关系模板代码

    以下罗列了使用关系型数据库中常见关系定义模板代码 一对多示例场景:用户与其发布的帖子(用户表与帖子表)角色与所属于该角色的用户(角色表与多用户表)示例代码class Role(db.Model): & ...

  8. [KVM][guestfs] 安装 guestfs-python 出错

    pip install http://download.libguestfs.org/python/guestfs-1.36.13.tar.gz 执行后出错: 然后百度.谷歌,都是说安装 gcc 或者 ...

  9. 【uva1658 算法竞赛入门经典】海军上将【费用流】

    题意 给出一个v(3<=v<=1000)个点e(3<=e<=10000)条边的有向加权图,求1-v的两条不相交(除了起点和终点外没有公共点)的路径,使得权和最小. 分析 费用流 ...

  10. 关于@property与@syntheszie的使用问题

    写在前面:在ARC大行其道的“现代化社会”,不少人不再对“完整的“OC”抱有它应该获得的尊重,于是浮躁成了代名词~~ 在使用ARC时,大家声明变量的过程中,往往使用@property来通过编译器,隐式 ...