LeetCode OJ:Maximum Subarray(子数组最大值)
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(子数组最大值)的更多相关文章
- leetCode 53.Maximum Subarray (子数组的最大和) 解题思路方法
Maximum Subarray Find the contiguous subarray within an array (containing at least one number) whic ...
- [array] leetcode - 53. Maximum Subarray - Easy
leetcode - 53. Maximum Subarray - Easy descrition Find the contiguous subarray within an array (cont ...
- 小旭讲解 LeetCode 53. Maximum Subarray 动态规划 分治策略
原题 Given an integer array nums, find the contiguous subarray (containing at least one number) which ...
- 【js】Leetcode每日一题-子数组异或查询
[js]Leetcode每日一题-子数组异或查询 [题目描述] 有一个正整数数组 arr,现给你一个对应的查询数组 queries,其中 queries[i] = [Li, Ri]. 对于每个查询 i ...
- [LeetCode] 53. Maximum Subarray 最大子数组
Given an integer array nums, find the contiguous subarray (containing at least one number) which has ...
- C#解leetcode 53.Maximum Subarray
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- [LeetCode] 53. Maximum Subarray 解题思路
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- [LeetCode] 53. Maximum Subarray 最大子数组 --动态规划+分治
Given an integer array nums, find the contiguous subarray (containing at least one number) which has ...
- 41. leetcode 53. Maximum Subarray
53. Maximum Subarray Find the contiguous subarray within an array (containing at least one number) w ...
- LeetCode 53. Maximum Subarray(最大的子数组)
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
随机推荐
- MariaDB备份和恢复
一.为什么要备份? 灾难恢复:硬件故障.软件故障.自然灾害.黑客攻击.误操作: 测试二.要注意的要点: 能容忍最多丢失多少数据: 恢复数据需要多长时间内完成: 需要恢复哪些数据: (1)做还原测试,用 ...
- beego——过滤器
beego支持自定义过滤中间件,例如安全验证.强制跳转等. 过滤器函数如下所示: beego.InsertFilter(pattern string, position int, filter Fil ...
- Canvas:橡皮筋线条绘制
Canvas:橡皮筋线条绘制 效果演示 实现要点 事件监听 [说明]: 在Canvas中检测鼠标事件是非常简单的,可以在canvas中添加一个事件监听器,当事件发生时,浏览器就会调用这个监听器. 我们 ...
- python之路 堡垒机paramiko
paramiko 1.安装 pip3 install paramiko 二.使用 SSHClient 用于连接远程服务器并执行基本命令 基于用户名密码连接: import paramiko # 创建S ...
- Windows安装多个Tomcat服务
1.下载tomcat解压,并复制三份(用压缩版的不要用安装版的) 2.配置环境变量CATALINA_HOME和CATALINA_BASE .改端口 修改文件server.xml,修改3个端口号 < ...
- 【c++ primer, 5e】类的其他特性(卒)
1 - Class Members Revisited 2 - Functions That Return *this 3 - Class Types 4 - Friendship Revisited ...
- Apache 浏览器访问限制配置
浏览器访问限制配置 user_agent收入的浏览器中,我们通过百度,谷歌很容易就可以查到相关的一些资料,方便了我们对知识的查找,但在某些特定情况下,我们并不希望有人可以通过某写搜索引擎直接访问到我们 ...
- Java四则运算总结
Java结对编程四则运算第二周总结 需求分析 要随机产生规定数量的式子. 要对分式进行计算. 要支持多语言. 要自动判断对错. 设计思路 确定要选用的语言: 要产生随机数: 在产生随机数的时候就要产生 ...
- Extracting info from VCF files
R, Bioconductor filterVcf: Extract Variants of Interest from a Large VCF File (Paul Shannon) We demo ...
- 主机不能访问虚拟机web服务的问题
虚拟机是CentOs 7 iptables -Fiptables -P INPUT ACCEPT 参考:http://blog.csdn.net/abnereel/article/details/41 ...