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. Java并发编程的艺术(十二)——线程安全

    1. 什么是『线程安全』? 如果一个对象构造完成后,调用者无需额外的操作,就可以在多线程环境下随意地使用,并且不发生错误,那么这个对象就是线程安全的. 2. 线程安全的几种程度 线程安全性的前提:对『 ...

  2. 为何要对URL进行编码

    为何要对URL进行编码 我们都知道Http协议中参数的传输是"key=value"这种简直对形式的,如果要传多个参数就需要用“&”符号对键值对进行分割.如"?na ...

  3. [转]ThinkPHP中如何使用原生SQL

    From : http://huangqiqing123.iteye.com/blog/1540323 TP的模型可以支持原生SQL操作,提供了query和execute两个方法,为什么原生SQL还要 ...

  4. 如何使用pycharm调试(debug) django的测试用例?

    一.django应用或者flask应用的调试: 结合debug模式,在代码处添加断点,即可实现断点调试功能 二. django应用或者flask应用测试用例的调试: 一般django应用的测试用例执行 ...

  5. Newtonsoft.Json高级用法DataContractJsonSerializer,JavaScriptSerializer 和 Json.NET即Newtonsoft.Json datatable,dataset,modle,序列化

    原文地址:https://www.cnblogs.com/yanweidie/p/4605212.html Newtonsoft.Json介绍 在做开发的时候,很多数据交换都是以json格式传输的.而 ...

  6. 回顾 Exchange 2007 SCC 安装-供需要的人参考!

    最近可能会涉及到一个项目的升级,客户目前是基于SCC+SCR的一种工作模式,因为之前对SCR 了解很少,所以需要搭建一个SCC+SCR 平台来做一个整体的POC,来还原整个安装和升级过程. 首先我们先 ...

  7. C++ assert 断言使用

    在研究corepattern需要让程序出core, 因此用到的assert, 记录一下. 写严谨代码时,也可以使用assert进行严格的条件判断.   函数原型: #include <asser ...

  8. Easyui1.3.4+IIS6.0+IE8兼容问题解决

    刚刚学习JQuery Easyui,就遇到了拦路虎,最新版本1.3.4下载下来部署到win2003 + IIS6.0中发现所有demo都不可以渲染,IE8提示错误如下: 详细内容如下: 用户代理: M ...

  9. Win7局域网打印机共享设置(详细图文流程)

    本文将向读者说明在Win7下如何实现同个局域网内共享打印机.经过测试,Win7之间和Win7与XP之间均可正常连接. 第一步:取消禁用Guest用户 1. 点击[开始]按钮,在[计算机]上右键,选择[ ...

  10. maskrcnn_benchmark代码分析(3)

    数据结构 数据加载 数据后处理