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 ...
随机推荐
- SSIS中出现数据流数据源假死状态的解决办法
相信开发过Sql Server SSIS的人都遇到过在数据流中数据源假死的问题,特别是Excel Source特别容易假死,当job执行到数据流中的Excel Source时,既不报错也不执行,也没有 ...
- DataGuard相关视图
1.v$database SELECT name,open_mode,database_role,protection_mode,protection_level FROM v$database; 其 ...
- eclipse安装activiti插件
参考: https://blog.csdn.net/augustaurora/article/details/59618737 https://blog.csdn.net/qq_33547950/ar ...
- JBDC—③数据库连接池的介绍、使用和配置
首先要知道数据库连接(Connection对象)的创建和关闭是非常浪费系统资源的,如果是使用常规的数据库连接方式来操作数据库,当用户变多时,每次访问数据库都要创建大量的Connnection对象,使用 ...
- css中的定位问题
由于我最近在修改自己的网页布局,突然发现了自己对css中的定位概念还是混淆的,于是通过查官方文档,大神博客,自己实践,重新梳理了css定位的知识点.如果有不对的地方,请指正
- SQL注入总结篇
分类SQL注入的攻击方式根据应用程序处理数据库返回内容的不同,可以分为可显注入.报错注入和盲注. 可显注入攻击者可以直接在当前界面内容中获取想要获得的内容. 报错注入数据库查询返回结果并没有在页面中显 ...
- 郁金香指标开源库的使用--(tulipindicators-0.8.4)
瞎逛发现最新出了这么一个指标库,有100多种指标的函数库,文档写的比较好,重要的是作者一直在维护. 把它编成库,然后测试一下,可用于自动交易,策略交易等开发. 1.下载地址 https://githu ...
- 鼠标移动在屏幕上显示温度Tip提示功能-CToolTipCtrl类的使用
初学VC++,太多知识不懂,需要不断的查找资料,想通过记录让自己有所积累,主要是怕以后会很快忘记.最近在做一个在屏幕上显示鼠标移动位置的温度值,我利用先缓存一帧图像的温度值,然后,通过鼠标移动消息相应 ...
- .Net 两个对像之间的映射 ( 二 )
一.使用 class Program { static void Main(string[] args) { User u1 = new User(); u1.UserName = "aaa ...
- 20155217 2016-2017-2 《Java程序设计》第3周学习总结
20155217 2016-2017-2 <Java程序设计>第3周学习总结 教材学习内容总结 第四章 要产生对象必须先定义类,类定义时使用class关键词,建立实例要使用new关键词. ...