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.

=====================
状态转移方程为:
/**dp
        f[i] = max{f[i-1]+nums[i],nums[i]},1<=i<n
        return max{f[i]} 0<=i<n
        */
---------
=========
code
class Solution {
public:
int maxSubArray(vector<int> &nums){
/**dp
f[i] = max{f[i-1]+nums[i],nums[i]},1<=i<n
return max{f[i]} 0<=i<n
*/ int length = nums.size();
vector<int> f(length);
f[] = nums[];
for(int i = ;i<length;i++){
f[i] = max(f[i-]+nums[i],nums[i]);
} vector<int>::iterator iter;
iter = max_element(f.begin(),f.end());
return *iter;
}
};
 

53. Maximum Subarray的更多相关文章

  1. [Leetcode][Python]53: Maximum Subarray

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 53: Maximum Subarrayhttps://leetcode.co ...

  2. 41. leetcode 53. Maximum Subarray

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

  3. 53. Maximum Subarray【leetcode】

    53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...

  4. [array] leetcode - 53. Maximum Subarray - Easy

    leetcode - 53. Maximum Subarray - Easy descrition Find the contiguous subarray within an array (cont ...

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

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

  6. 小旭讲解 LeetCode 53. Maximum Subarray 动态规划 分治策略

    原题 Given an integer array nums, find the contiguous subarray (containing at least one number) which ...

  7. LN : leetcode 53 Maximum Subarray

    lc 53 Maximum Subarray 53 Maximum Subarray Find the contiguous subarray within an array (containing ...

  8. Leetcode之53. Maximum Subarray Easy

    Leetcode 53 Maximum Subarray Easyhttps://leetcode.com/problems/maximum-subarray/Given an integer arr ...

  9. leetcode 53. Maximum Subarray 、152. Maximum Product Subarray

    53. Maximum Subarray 之前的值小于0就不加了.dp[i]表示以i结尾当前的最大和,所以需要用一个变量保存最大值. 动态规划的方法: class Solution { public: ...

  10. LeetCode练题——53. Maximum Subarray

    1.题目 53. Maximum Subarray——Easy Given an integer array nums, find the contiguous subarray (containin ...

随机推荐

  1. jquery.uploadify 动态传递参数

    最近 项目中使用到 uplaodify 来实现上传文件的功能.在传输动态参数的时候,遇到了问题! 使用官网提供的 settings 方法 官方例子function changeBtnText() {  ...

  2. C++数组小知识

    数组大小 我们一般情况下可以使用sizeof(数组名)/sizeof(数组元素)求数组元素个数,但需要注意的是,当我们需要调用函数处理数组时,数组的长度要在调用函数之前获取,因为调用函数的时候,数组退 ...

  3. String类和StringBuffer类的方法

    一.String类的方法 public char charAt(int index)          返回字符串index个字符 public int length()         返回字符串长 ...

  4. Java—接口与抽象类

    一.接口 Java编程语言中禁止多继承属性,但可以通过接口来帮助类扩展方法.接口中可以定义大量的常量和方法,但其中的方法只是一种声明,没有具体的实现,使用接口的类自己实现这些方法. 接口与类的不同在于 ...

  5. [转]深入理解Java 8 Lambda(语言篇——lambda,方法引用,目标类型和默认方法)

    以下内容转自: 作者:Lucida 微博:@peng_gong 豆瓣:@figure9 原文链接:http://zh.lucida.me/blog/java-8-lambdas-insideout-l ...

  6. CTSC2015 酱油记

    终于又到写酱油记的时间了...不过开心不起来诶.. Day 0 晚上睡不着觉也不造为啥... 起来看了一本亚里亚小说,继续睡,睡不着... 又起来看了一本亚里亚小说,继续睡,睡不着... 然后...死 ...

  7. bootstrap学习<三>打开模态窗体

    可以切换模态框(Modal)插件的隐藏内容: 通过 data 属性:在控制器元素(比如按钮或者链接)上设置属性 data-toggle="modal",同时设置 data-targ ...

  8. [windows操作系统]windows管理

    1.磁盘管理: 1.1.使用DISKPART命令行工具创建扩展分区: windows自带有一个disk management(磁盘管理)工具,但在其中却找不到如何创建扩展分区(一般MBR分区格式需要扩 ...

  9. Java中的Scanner类和String类

    1:Scanner的使用(了解)    (1)在JDK5以后出现的用于键盘录入数据的类. (2)构造方法: A:讲解了System.in这个东西.            它其实是标准的输入流,对应于键 ...

  10. OutputCache 如何使用本地缓存 【转】

    注意!ASP.NET MVC 3 的一个 OutputCache 问题   在用 ASP.NET MVC 3 重写博客园网站首页时,特地留意了一下这个缓存问题,通过这篇博文分享一下. 在 ASP.NE ...