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 ...
随机推荐
- 微服务之数据同步Porter
Porter是一款数据同步中间件,主要用于解决同构/异构数据库之间的表级别数据同步问题. 背景 在微服务架构模式下深刻的影响了应用和数据库之间的关系,不像传统多个服务共享一个数据库,微服务架构下每个服 ...
- Docker permission denied
Issue: Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/ ...
- [iOS]UIFont的lineHeight与pointSize
写这篇文章的初衷是由于我有一次想获取一个font的字体大小时使用了UIFont的lineHeight属性,结果是比字体的大小要大了一部分,然后经过查阅各种资料,发现了原因. 首先我们来看一看UIFon ...
- canvas制作的烟花效果
最近感觉canvas挺有意思的,在业余时间没事研究了一下,参考过网上一些思路,话不多说,开始啦. github地址:https://github.com/aWhiteBear/fireworks 演示 ...
- 01.centos7环境准备
博客为日常工作学习积累总结: 1.环境准备: 系统版本:CentOS-7-x86_64-Minimal-1810.iso 运行环境:虚拟机windows上的VM 15 系统安装:参照老男孩运维要求 2 ...
- es6 入坑笔记(二)---函数扩展,箭头函数,扩展运算符...
函数扩展 1.函数可以有默认值 function demo( a = 10,b ){} 2.函数可以使用解构 function demo( { a = 0,b = 0 } = {} ){ } 3.函数 ...
- swiper 导航有多个,被点击的项居中显示。
<div class="swiper-container"> <div class="swiper-wrapper"> <div ...
- Delphi判断某个类是否实现了某个接口
通过TObject.GetInterface可以获得对象的实例实现某个接口,前提条件是必须实例化对象后才能运行GetInterface 下面的方法可获取类是否实现了某个接口,并返回接口的偏移: fun ...
- 我的 Delphi 学习之路 —— Delphi 助手的安装
标题:我的 Delphi 学习之路 -- Delphi 助手的安装 作者:断桥烟雨旧人伤 Delphi 助手的安装 CnWizards 类似于 VS 中的番茄助手,在编写 Delphi 代码时帮助极大 ...
- Flume(5)-Ganglia监控
一. 安装Ganglia 1. 安装httpd服务与php sudo yum -y install httpd php 2. 安装其他依赖 sudo yum -y install rrdtool pe ...