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.

click to show more practice.

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.

贪心算法,当连续和sum>0时,sum+=A[i],负责sum=A[i]

class Solution {
public:
int maxSubArray(int A[], int n) {
int max=A[],sum=;
for(int i=;i<n;++i)
{
if(sum<)
sum=A[i];
else
sum+=A[i];
max=max<sum?sum:max;
}
return max;
}
};

maximum-subarray 序列最大连续和 贪心的更多相关文章

  1. Maximum Subarray(最大连续子序列和)

    https://leetcode.com/problems/maximum-subarray/ 思路: 如果全为负值,那么取最大值 如果有非负值,那么我们依次计算到当前位置为止的最大值.假设有n个元素 ...

  2. 算法:寻找maximum subarray

    <算法导论>一书中演示分治算法的第二个例子,第一个例子是递归排序,较为简单.寻找maximum subarray稍微复杂点. 题目是这样的:给定序列x = [1, -4, 4, 4, 5, ...

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

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

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

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

  5. 53. Maximum Subarray最大求和子数组12 3(dp)

    [抄题]: Find the contiguous subarray within an array (containing at least one number) which has the la ...

  6. 【leetcode】Maximum Subarray (53)

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

  7. 【leetcode】Maximum Subarray

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

  8. 3月7日 Maximum Subarray

    间隔2天,继续开始写LeetCodeOj. 原题: Maximum Subarray 其实这题很早就看了,也知道怎么做,在<编程珠玑>中有提到,求最大连续子序列,其实只需要O(n)的复杂度 ...

  9. Maximum Subarray Sum

    Maximum Subarray Sum 题意 给你一个大小为N的数组和另外一个整数M.你的目标是找到每个子数组的和对M取余数的最大值.子数组是指原数组的任意连续元素的子集. 分析 参考 求出前缀和, ...

随机推荐

  1. 超人前传第一至十季/全集Smallville迅雷下载

    超人前传 第一至十季 Smallville Season 1-10 (2001-2010)本季看点:CW台的长寿剧<超人前传>在以帅哥靓女征服观众了这么多年后,也终于进入尾声,该剧将于今年 ...

  2. Java并发编程的艺术(五)——中断

    什么是中断? 在Java中没有办法立即停止一条线程,然而停止线程却显得尤为重要,如取消一个耗时操作.因此,Java提供了一种用于停止线程的机制——中断. 中断只是一种协作机制,Java没有给中断增加任 ...

  3. 详细解读Volley(四)—— 自定义Request

    Volley中提供了几个Request,如果我们有特殊的需求,完全可以自定义Request的,自定义Request自然要继承Request,那么本篇就教大家来一步一步地定义一个自己的Request类. ...

  4. [DEFCON全球黑客大会] CTF(Capture The Flag)

    copy : https://baike.baidu.com/item/ctf/9548546?fr=aladdin CTF(Capture The Flag)中文一般译作夺旗赛,在网络安全领域中指的 ...

  5. ECC校验

    ECC的全称是 Error Checking and Correction or Error correction Coding,是一种用于差错检测和修正的算法.NAND闪存在生产和使用中都会产生坏块 ...

  6. Netty Message RefCount

    ByteBuf is always reference counted To control the life cycle of a ByteBuf in a more predictable way ...

  7. arcgis server 10 for java 8399根目录是404的提示取消,并跳转到 地图目录 /arcgis/rest/services下

    看了Howto: 取消ArcGIS Server 9.x for Java内置tomcat在8399端口的文件列表 http://support.esrichina-bj.cn/2009/0819/9 ...

  8. 案例导入和导出Scott用户

    ylbtech-Oracle:案例导入和导出Scott用户  导入和导出Scott用户 1. 导出Scott用户下的所有对象返回顶部 1.1, Microsoft Windows [版本 ] 版权所有 ...

  9. Hadoop概念学习系列之Hadoop、Spark学习路线(很值得推荐)

    说在前面的话 此笔,对于仅对于Hadoop和Spark初中学者.高手请忽略! 1 Java基础: 视频方面:          推荐<毕向东JAVA基础视频教程>.学习hadoop不需要过 ...

  10. js面向对象之继承-原型继承

    //animal 父类 超类 var Animal = function(name) { this.name = name; this.sayhello = function() { alert(&q ...