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 ...
随机推荐
- 每天to do list
至少写一页书 写代码做一个实验 读10+页专业书 一年时间,如果经济状况没有改善的话,回归企业.
- 【原创】如何使用Jmockit进行单元测试
如何使用jmockit进行单元测试 1. Jmockit简介 JMockit 是用以帮助开发人员编写测试程序的一组工具和API,它完全基于 Java 5 SE 的 java.lang.instrume ...
- ubuntu SDL2 安装时依赖文件导致安装失败
今天打算学习littlev GUI,使用Ubuntu来实现仿真,然后在安装SDL2的时候,始终因为依赖关系导致安装失败,我尝试手动去安装那些有依赖关系的包发现根本不可行,然后我百度上也没有找到合适的法 ...
- scrapy基础
scrapy Scrapy 是用 Python 实现的一个为了爬取网站数据.提取结构性数据而编写的应用框架. Scrapy 常应用在包括数据挖掘,信息处理或存储历史数据等一系列的程序中. Scrapy ...
- 数据结构09—— 并查集(Union-Find)
一.关于并查集 并查集(Union-Find)是一种树型的数据结构,常用于处理一些不相交集合(Disjoint Sets)的合并及查询问题.并查集(Union-Find)从名字可以看出,主要它涉及两种 ...
- Linux学习-计算机基础
Linux 学习-计算机基础 一.描述计算机的组成及其功能. 计算机系统是由硬件(Hardware)和软件(Software )两部分组成. 硬件: 从硬件基本结构上来讲,计算机是由运算器.控制器.存 ...
- ruby安装devkit
双击下载文件,指定解压路径,路径中不能有空格.如C:\DevKit,这个路径就是<DEVKIT_INSTALL_DIR>. > cd <DEVKIT_INSTALL_DIR&g ...
- C语言链栈
链栈与链表结构相似 typedef int elemtype; typedef struct linkedStackNode{ elemtype e; struct linkedStackNode * ...
- nodejs fastdfs
node端fastdfs客户端上传文件 var FdfsClient = require('fdfs'); var fdfs = new FdfsClient({ // tracker servers ...
- 【课堂实践】Myod
实验内容 编写MyOD.java 用java MyOD XXX实现Linux下od -tx -tc XXX的功能 实验代码 od.java 截图 遇到的问题及解决办法 一开始想的方向是将得出的功能结果 ...