53. [LeetCode] Maximum Subarray
Given an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum.
Example:
Input: [-2,1,-3,4,-1,2,1,-5,4],
Output: 6
Explanation: [4,-1,2,1] has the largest sum = 6.
Follow up:
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 ans=nums[],sum=;
for(int i=; i<nums.size();i++){
sum+=nums[i];
ans=max(ans,sum);
sum=max(sum,);
}
return ans;
}
};
//Idea is very simple. Basically, keep adding each integer to the sequence until the sum drops below 0.
//If sum is negative, then should reset the sequence.
53. [LeetCode] Maximum Subarray的更多相关文章
- LEETCODE —— Maximum Subarray [一维DP]
Maximum Subarray Find the contiguous subarray within an array (containing at least one number) which ...
- LeetCode: Maximum Subarray 解题报告
Maximum Subarray Find the contiguous subarray within an array (containing at least one number) which ...
- [LeetCode]Maximum Subarray题解
Maximum Subarray: Find the contiguous subarray within an array (containing at least one number) whic ...
- [LeetCode] Maximum Subarray Sum
Dynamic Programming There is a nice introduction to the DP algorithm in this Wikipedia article. The ...
- leetcode || 53、Maximum Subarray
problem: Find the contiguous subarray within an array (containing at least one number) which has the ...
- [LeetCode]题53:Maximum Subarray
Given an integer array nums, find the contiguous subarray (containing at least one number) which has ...
- (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 large ...
- 【LeetCode算法-53】Maximum Subarray
Given an integer array nums, find the contiguous subarray (containing at least one number) which has ...
随机推荐
- 分布式一致性协议-2PC与3PC(二)
一.分布式一致性 一个事务需要跨多个分布式节点,又要保持事务的ACID特性,需要引入协调者来统一调度所有分布式节点的执行逻辑,被调度的节点称为参与者. 协调者负责调用参与者,并决定最终是否提交事务.基 ...
- iOS之限制TextField的输入长度
网上有很多限制textField输入长度方法,但是我觉得都不是很完美,准确来说可以说是不符合实际开发的要求,因此在这里整理一下textField限制输入长度的方法.我所采用的并不是监听方法而是最不同的 ...
- SQL语句group by 与order by 执行顺序引发的一场“内斗”
直入主题!看看下面这SQL会不会报错?如果报错应该是什么错误! --说明:黑色字体都是列SELECT application_id, index_num, num, amount FROM `cred ...
- 关于if与switch的使用与区别
这是if语句: if (条件表达式1){ //条件判断 //n多语句1 }else if(条件表达式2){ //n多语句2 }else if(条件表达式3){ //n多语句3 } ... ... el ...
- 关于pythond在终端中运行
下载python并安装后,如果想要在终端中直接运行,我们需要配置环境变量. 在计算机右击选择属性,,选择高级属性,点击环境变量,,即可新建环境变量, ,然后可以在终端中运行python解释器.
- Python学习 :面向对象 -- 类的成员
类的成员 一.字段 - 字段可以分为'普通字段'和'静态字段'两种 - 普通字段(保存在对象中,执行时只能通过对象来进行访问) - 静态字段(保存在类中,在内存中只保存一份数据(可以有效地节约内存的空 ...
- scala 求数组排序后每两个元素的差值
求数组排序后每两个元素的差值 例如数组 1,5,8,10,2 求得结果为 1,3,3,2 一般什么样的场景会有这种需求呢? 比如 计算一堆数据在一定时间内的计算时延, 或者得到这段时间内数据的平均计算 ...
- HIVE-分桶表的详解和创建实例
我们学习一下分桶表,其实分区和分桶这两个概念对于初学者来说是比较难理解的.但对于理解了的人来说,发现又是如此简单. 我们先建立一个分桶表,并尝试直接上传一个数据 buckets row format ...
- 1130: [POI2008]POD Subdivision of Kingdom
1130: [POI2008]POD Subdivision of Kingdom https://lydsy.com/JudgeOnline/problem.php?id=1130 分析: 有效状态 ...
- directive 指令一
什么是Directive Directive将一段html,js封装在一起,形成一个可以复用的独立个体,具有特定的功能.angularjs中的指令通常是比较小的组件,它相当于是给我们提供了一些公共的自 ...