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 ...
随机推荐
- phpcms使用session的方法
phpcms使用session //session开始 必须有下面的代码,否则无效 private function _session_start() { $session_storage = 'se ...
- 我们为什么使用ORM
我们为什么使用ORM? http://www.cnblogs.com/tansm/archive/2006/06/07/419927.html 博客园在推广ORM方面的确做了很大的贡献,很多的程序员开 ...
- mybatis 插入之后返回id
usermapper.xml <insert id="insert" parameterType="app.models.User" keyPropert ...
- Codeforces Round #396 (Div. 2) C. Mahmoud and a Message
地址:http://codeforces.com/contest/766/problem/C 题目: C. Mahmoud and a Message time limit per test 2 se ...
- C# 获取当前星期几
private static string GetWeekName() { string weekName = ""; switch (DateTime.Now.DayOfWeek ...
- Dubbo之RPC架构
为什么会有dubbo的出现: 随着互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式服务架构以及流动计算架构势在必行,亟需一个治理系统确保架构有条不紊的演进. 单一应用架构 当 ...
- nginx日志分割总结
nginx日志自己不会进行分个,所有日志都会累积的记录在 access.log,error.log 中,当请求量大,一天就能到几百兆,如果不进行分给,对日志的查看和写入性能都有影响. 1. 编写脚本n ...
- JS 闭包应用
1. 代替全局变量 //闭包应用1:代替全局变量的使用 //多个函数都用到一个变量,通常我们会定义一个全局变量,然后在各函数中应用它,//为了避免使用全局变量,可以通过使用立即执行函数定义临时变量,子 ...
- Java通过JDBC操作Hive
http://www.cnblogs.com/netbloomy/p/6688670.html 0.概述 使用的都是CLI或者hive –e的方式仅允许使用HiveQL执行查询.更新等操作.然而Hiv ...
- 间隔问题,合并间隔(merge interval),插入间隔(insert interval)
Merge Interval: Given a collection of intervals, merge all overlapping intervals. For example,Given ...