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.

典型的DP问题,递推条件还是想了有点长时间,代码如下所示:

 class Solution {
public:
int maxSubArray(vector<int>& nums) {
vector<int> ans;
int sz = nums.size();
if(!sz) return ;
ans.resize(sz);
ans[] = nums[];
int maxSum = nums[];
for(int i = ; i < sz; ++i){
ans[i] = max(nums[i], ans[i - ] + nums[i]);  //这里的条件应该注意
maxSum = max(ans[i], maxSum);
}
return maxSum;
}
};

LeetCode OJ:Maximum Subarray(子数组最大值)的更多相关文章

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

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

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

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

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

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

  4. 【js】Leetcode每日一题-子数组异或查询

    [js]Leetcode每日一题-子数组异或查询 [题目描述] 有一个正整数数组 arr,现给你一个对应的查询数组 queries,其中 queries[i] = [Li, Ri]. 对于每个查询 i ...

  5. [LeetCode] 53. Maximum Subarray 最大子数组

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

  6. C#解leetcode 53.Maximum Subarray

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

  7. [LeetCode] 53. Maximum Subarray 解题思路

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

  8. [LeetCode] 53. Maximum Subarray 最大子数组 --动态规划+分治

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

  9. 41. leetcode 53. Maximum Subarray

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

  10. LeetCode 53. Maximum Subarray(最大的子数组)

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

随机推荐

  1. PAT 1035 Password [字符串][简单]

    1035 Password (20 分) To prepare for PAT, the judge sometimes has to generate random passwords for th ...

  2. Codeforces Round #305 (Div. 2)

    C. Mike and Frog 题意:有一只青蛙和一朵花,分别高度为h1.h2,每浇一次水,h1=(x1*h1+y1)mod m,h2=(x2*h2+y2)mod m.求最少浇多少次后h1=a1,h ...

  3. 扯一扯 C#委托和事件?策略模式?接口回调?

    早前学习委托的时候,写过一点东西,今天带着新的思考和认知,再记点东西.这篇文章扯到设计模式中的策略模式,观察者模式,还有.NET的特性之一--委托.真的,请相信我,我只是在扯淡...... 场景练习 ...

  4. NIO复习02

    Selector 1. Selector(选择器)是Java NIO中能够检测一到多个NIO通道,并能够知晓通道是否为诸如读写事件做好准备的组件.这样,一个单独的线程可以管理多个channel,从而管 ...

  5. 面试:做过sql优化吗?

    近来面试找工作经常会遇见这种问题: 做过数据库优化吗?大数据量基础过吗?系统反应慢怎么查询? 这咱也没背过啊,面试还老问,现在的网站主要的压力都来自于数据库,频繁的数据库访问经常会使系统瘫痪,这样就需 ...

  6. Secondary ,Supplementary alignment 和bwa mem的-M -Y参数

    1.supplementary alignment supplementary alignment是指一条read的一部分和参考区域1比对成功,另一部分和参考区域2比对成功,参考区域1和参考区域2没有 ...

  7. ==与equals的各种情况

    == 能用于基本类型之间.基本类型与引用类型之间及相同引用类型之间,不能用于不同引用类型之间 对于基本类型,取值来对比,对于引用类型,取地址来对比 int a= 1; Integer b= 1; Sy ...

  8. session/token/cookie/socket区别

    Session:在计算机中,尤其是在网络应用中,称为“会话控制”.Session 对象存储特定用户会话所需的属性及配置信息.这样,当用户在应用程序的 Web 页之间跳转时,存储在 Session 对象 ...

  9. git常用的语句

    下面总结出开发中常用的指令: 1.git init:初始化git仓库 2.git add 文件名:把文件添加到暂存区中 3.git commit -m "提交信息":提交暂存区内容 ...

  10. mysql 一些属性

    1)定义id,设置int,涉及的属性有: BINARY二进制 UNSIGNED无符号数 UNSIGNED ZEROFILL 在列字段中使用UNSIGNED ZEROFILL属性,如: 插入int(4) ...