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.

此题为经典的求连续子数组最大和问题,除了暴力的解法,想办法用O(n)时间来做。

直接贴代码:

 class Solution {
public:
int maxSubArray(int A[], int n) {
if(A == NULL || n < )
return -; int max = A[];
int temp = ; for(int i = ; i < n ; i++){
if(temp < )
temp = A[i];
else
temp += A[i]; if(temp > max)
max = temp;
} return max; }
};

Maximum Subarray 连续子数组最大和的更多相关文章

  1. 连续子数组最大和(python)

    题目描述 HZ偶尔会拿些专业问题来忽悠那些非计算机专业的同学.今天测试组开完会后,他又发话了:在古老的一维模式识别中,常常需要计算连续子向量的最大和,当向量全为正数的时候,问题很好解决.但是,如果向量 ...

  2. LeetCode OJ:Maximum Subarray(子数组最大值)

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

  3. 【剑指offer】连续子数组最大和

    思路dp很清楚,就是要注意细节. int FindGreatestSumOfSubArray(vector<int> array) { ; ], tempsum = array[]; // ...

  4. 剑指Offer29 连续子数组最大和

    /************************************************************************* > File Name: 29_Greate ...

  5. 【剑指offer】连续子数组的最大和,C++实现

    原创博文,转载请注明出处!本题牛客网地址 博客文章索引地址 博客文章中代码的github地址 # 题目       输入一个整形数组,数组里有正数也有负数.数组中的一个或连续多个整数组成一个子数组.求 ...

  6. 每日一题 - 剑指 Offer 42. 连续子数组的最大和

    题目信息 时间: 2019-06-30 题目链接:Leetcode tag: 动态规划 难易程度:简单 题目描述: 输入一个整型数组,数组里有正数也有负数.数组中的一个或连续多个整数组成一个子数组.求 ...

  7. 剑指 Offer 42. 连续子数组的最大和

    题目描述 输入一个整型数组,数组中的一个或连续多个整数组成一个子数组.求所有子数组的和的最大值. 要求时间复杂度为\(O(n)\). 示例1: 输入: nums = [-2,1,-3,4,-1,2,1 ...

  8. 剑指 Offer 42. 连续子数组的最大和 + 动态规划

    剑指 Offer 42. 连续子数组的最大和 题目链接 状态定义: 设动态规划列表 \(dp\) ,\(dp[i]\) 代表以元素 \(4nums[i]\) 为结尾的连续子数组最大和. 为何定义最大和 ...

  9. 【LeetCode】Maximum Product Subarray 求连续子数组使其乘积最大

    Add Date 2014-09-23 Maximum Product Subarray Find the contiguous subarray within an array (containin ...

随机推荐

  1. [BZOJ 5155][Tjoi2014]电源插排

    传送门 网上大部分题解都写得是动态开点线段树,然而像\(MiEcoku\)这么懒惰的显然不会去写线段树... \(\color{green}{solution}\) 我们考虑来点骚操作. 线段树维护的 ...

  2. [转] js画图开发库--mxgraph--[graphlayout-图形布局.html]

    [From] http://chwshuang.iteye.com/blog/1797740 布局变化,下方还有动画效果选项: <!Doctype html> <html xmlns ...

  3. 开源项目-Aiguille

        项目地址: https://github.com/wwkai555/Aiguille 这个项目主要使用Android L新特性 - 最新的widget以及一些值得推荐和使用的开源库比如butt ...

  4. 三、OPENERP 中的对象关系类型

    OE中的对象关系一共分四种,one2one,one2many,many2one,many2many.他们的意思分别是一对一,一对多,多对一以及多对多. 我们新建一个模块来测试这四种类型 1.one2o ...

  5. java容器类1:Collection,List,ArrayList,LinkedList深入解读

    1. Iterable 与 Iterator Iterable 是个接口,实现此接口使集合对象可以通过迭代器遍历自身元素. public interface Iterable<T> 修饰符 ...

  6. PHP 判断字符串 是否 包含另一个字符串

    1.stristr 忽略大小写 $string = 'Hello World!'; if(stristr($string, 'earth') === FALSE) { echo '"eart ...

  7. 多线程编程(三)-CountDownLatch的使用

    CountDownLatch的介绍 类CountDownLatch是同步功能得一个辅助类,使用效果就是给定一个计数,当使用CountDownLatch类的线程判断计数不为0时,则呈wait状态,如果是 ...

  8. 我爱Markdown (3)

    继续Markdown的常见语法, 本文将介绍: 07 - Links 链接 08 - Images 图片 09 - Blockquotes 块引用 10 - Backslash Escapes 显示保 ...

  9. HighChart 体验之旅 (后台传递JSON参数和数据的方法)

    转自:http://www.cnblogs.com/daviddai/archive/2013/04/12/Highchart.html 官网:http://www.highcharts.com/ 中 ...

  10. Http协议之Content-Length

    前言 http协议是互联网中最重要的协议之一,虽然看上去很简单,但是实际中经常遇到问题,我们就已经遇到好几次了.有长连接相关的,有报文解析相关的.对http协议不能一知半解,必须透彻理解才行.所以就写 ...