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.

 class Solution {
public:
int maxSubArray(vector<int>& nums) {
int n=nums.size();
if(n<) return ;
int start=;
int sum=nums[];
for(int i=n-;i>=;i--)
{
start=max(nums[i],start+nums[i]);
sum=max(sum,start);
}
return sum;
}
};

Maximum Subarray的更多相关文章

  1. [LintCode] Maximum Subarray 最大子数组

    Given an array of integers, find a contiguous subarray which has the largest sum. Notice The subarra ...

  2. 【leetcode】Maximum Subarray (53)

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

  3. 算法:寻找maximum subarray

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

  4. LEETCODE —— Maximum Subarray [一维DP]

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

  5. 【leetcode】Maximum Subarray

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

  6. maximum subarray problem

    In computer science, the maximum subarray problem is the task of finding the contiguous subarray wit ...

  7. (转)Maximum subarray problem--Kadane’s Algorithm

    转自:http://kartikkukreja.wordpress.com/2013/06/17/kadanes-algorithm/ 本来打算自己写的,后来看到上述链接的博客已经说得很清楚了,就不重 ...

  8. 3月7日 Maximum Subarray

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

  9. LeetCode: Maximum Product Subarray && Maximum Subarray &子序列相关

    Maximum Product Subarray Title: Find the contiguous subarray within an array (containing at least on ...

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

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

随机推荐

  1. Spring(八)SSH整合简述

    一.Spring与Struts2的整合 1.1.整合步骤 1.2.配置web.xml 1.3.配置spring配置文件applicationContext.xml 1.4.配置struts配置文件 1 ...

  2. ios开发随笔第一篇-button,label按钮的一些属性的使用

    我用的是纯代码方式,喜欢用storyboard开发的其实也很爽了; 首先谈谈button,ios中新建button这个对象一般接触的都明白,UIButton *button名=[ UIButton a ...

  3. java 进制转换

    class Dec2XXX { public static void main(String[] args) { toBin(6); toHex(6); toOct(6); } /*10to2*/ p ...

  4. iOS之UI--涂鸦画板实例 (有待更新)

    首先是搭建框架 其他的略过,直接展示效果: 然后接下来上传搭建好两个控制器框架的源码百度云下载链接: http://pan.baidu.com/s/1skjpDox 密码: ardx ,工程里面还有我 ...

  5. opencms忘记Admin用户登录密码解决方案

    不知道现在还有多少人在用opencms: 我今天就是遇到一个问题,很久之前部署的一台opencms服务器,突然忘记密码了,记录下处理方法: 希望对你有帮助: opencms忘记Admin用户登录密码了 ...

  6. 【体系结构】Oracle参数介绍

    [体系结构]Oracle参数介绍 1  BLOG文档结构图 2  前言部分 2.1  导读和注意事项 各位技术爱好者,看完本文后,你可以掌握如下的技能,也可以学到一些其它你所不知道的知识,~O(∩_∩ ...

  7. Nodejs断言测试

    var assert = require('assert');/*node中,我们可以使用assert模块来测试代码.equal()和notEqual()分别作相等性和不等性的判断,第一个参数是期望值 ...

  8. CListCtrl中删除多个不连续的行

    ==================================声明================================== 本文原创,转载在正文中显要的注明作者和出处,并保证文章的完 ...

  9. lucene索引

    一.lucene索引 1.文档层次结构 索引(Index):一个索引放在一个文件夹中: 段(Segment):一个索引中可以有很多段,段与段之间是独立的,添加新的文档可能产生新段,不同的段可以合并成一 ...

  10. Merge compare columns when null

    Key words: merge compare columns when we contact merge sql in ETL, When we update some columns we sh ...