前言

 

【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. 概述XML

    xml概述--->干什么的 存储一对多的数据 作为配置文件存储数据 xml组成---->怎么用 元素的分类 包含标签体的标签(有开始标签和结束标签) 例如: <student> ...

  2. C经典实例

    1,九九乘法表 undefined reference to `sqrt' linux本身无math库 -lm,伪链接;

  3. Python开发丨这些面试题会不会难倒你

    1:以下的代码的输出将是什么? 说出你的答案并解释. class Parent(object):       x = 1   class Child1(Parent):       pass   cl ...

  4. python:数组/列表(remove()函数、append()函数、sort()函数、reverse()函数)

    排序: 1:整理顺序 #冒泡 lista = [5,7,11,19,99,63,3,9,1] list = [] while lista != []: number = 0 for i in list ...

  5. 火狐浏览器的RestClient,接口测试,Post提交数据

    昨天需要测试接口是不是调通,api中本身已经集成了测试,但加了OAuth,api有没有添加头文件,Headers的地方,所以想用RESTClient的Post提交重新测试下,但是,调了好几个小时都没有 ...

  6. AspectJ的aop编程--切入点表达式

  7. 201671010140. 2016-2017-2 《Java程序设计》java学习第十四周

      java学习第十四周       本周,主要精力放在了第十二章swing用户界面组件知识的学习,swing是一个用于开发Java应用程序用户界面的开发工具包.它以抽象窗口工具包(AWT)为基础使跨 ...

  8. Android——eclipse共享library以及导出jar包[转]

    目录(?)[-] 一apk之间共享Class 二apk导出jar包   android的apk在在eclipse上进行开发的时候,有时候需要import其它包中的一些class,正常的方法就是在jav ...

  9. boxing & unboxing

    [boxing & unboxing] Boxing is the process of converting a value type to the type object or to an ...

  10. if UNITY_EDITOR这个判断常用,还有哪个常用捏?

    #if DEVELOPMENT_BUILD || UNITY_EDITOR DEVELOPMENT_BUILD表示开发版的意思,会在程序右下角显示 Development Build 我们可以根据这个 ...