【LeetCode】53. Maximum Subarray (2 solutions)
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.
解法一:
如果当前子串和小于等于0,则归零重新开始累加。记录最大子串和。
注意:ret需要初始化为INT_MIN(以防所有都为负)。
因此需要定义为long long类型。以防INT_MIN加上一个负数溢出。
class Solution {
public:
int maxSubArray(int A[], int n) {
long long ret = INT_MIN;
long long cur = INT_MIN;
for(int i = ; i < n; i ++)
{
if(cur + A[i] > A[i])
cur += A[i];
else
cur = A[i];
ret = max(ret, cur);
}
return ret;
}
};

或者初始化为第一个值
class Solution {
public:
int maxSubArray(vector<int>& nums) {
int ret = nums[];
int cur = nums[];
for(int i = ; i < nums.size(); i ++)
{
cur = max(nums[i], cur+nums[i]);
ret = max(ret, cur);
}
return ret;
}
};

解法二:分治法。将数组分成两段A1,A2之后,就分解成为子问题了:
1、最大子串在A1中;
2、最大子串在A2中;
3、最大子串是A1后缀+A2前缀。
class Solution {
public:
int maxSubArray(int A[], int n) {
if(n == )
return A[];
else
{
int mid = n/;
//divide into [0~mid-1], [mid~n-1]
int ret1 = maxSubArray(A, mid);
int ret2 = maxSubArray(A+mid, n-mid);
int left = mid-;
int ret3_1 = A[mid-];
int temp = A[mid-];
left --;
while(left >= )
{
temp += A[left];
ret3_1 = max(ret3_1, temp);
left --;
}
int right = mid;
int ret3_2 = A[mid];
temp = A[mid];
right ++;
while(right < n)
{
temp += A[right];
ret3_2 = max(ret3_2, temp);
right ++;
}
return max(max(ret1, ret2), ret3_1+ret3_2);
}
}
};

【LeetCode】53. Maximum Subarray (2 solutions)的更多相关文章
- 【LeetCode】53. Maximum Subarray 最大子序和 解题报告(Python & C++ & Java)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力解法 动态规划 日期 题目地址: https:/ ...
- 【Leetcode】53. Maximum Subarray
题目地址: https://leetcode.com/problems/maximum-subarray/description/ 题目描述: 经典的求最大连续子数组之和. 解法: 遍历这个vecto ...
- 【LeetCode】053. Maximum Subarray
题目: Find the contiguous subarray within an array (containing at least one number) which has the larg ...
- 【一天一道LeetCode】#53. Maximum Subarray
一天一道LeetCode系列 (一)题目 Find the contiguous subarray within an array (containing at least one number) w ...
- 【leetcode】1186. Maximum Subarray Sum with One Deletion
题目如下: Given an array of integers, return the maximum sum for a non-empty subarray (contiguous elemen ...
- 【LeetCode】164. Maximum Gap (2 solutions)
Maximum Gap Given an unsorted array, find the maximum difference between the successive elements in ...
- 【LeetCode】718. Maximum Length of Repeated Subarray 解题报告(Python)
[LeetCode]718. Maximum Length of Repeated Subarray 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxu ...
- 【leetcode】998. Maximum Binary Tree II
题目如下: We are given the root node of a maximum tree: a tree where every node has a value greater than ...
- 【LeetCode】895. Maximum Frequency Stack 解题报告(Python)
[LeetCode]895. Maximum Frequency Stack 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxueming ...
随机推荐
- DELPHI中千万别直接使用CreateThread ,建议使用BeginThread(在C++中无大问题,可是到了DELPHI中情况就不一样了)
以前在写个别程序的时候老是喜欢使用纯API编程. 在C++中无大问题,可是到了DELPHI中情况就不一样了. 当你用 DELPHI写的多线程程序莫名其妙的内存错误,特别是字符串(string)操作; ...
- IOS学习笔记02---语言发展概述,计算机语言简介.
IOS学习笔记02---语言发展概述,计算机语言简介. ------------------------------------------------------------------------ ...
- 一键切换皮肤的解决思想及iframe嵌套时寻找下级iframe的方法
项目中有个一键切换皮肤的功能,感觉还不错,记录下,就是各颜色样式设置起来太复杂了,不知道有没有更简便的方法: 1.切换皮肤结构层 <li title="<s:text name= ...
- python3之日期和时间(转载)
转载:https://www.cnblogs.com/zhangxinqi/p/7687862.html a = datetime.datetime.now() time.sleep(10) b = ...
- hdu 5326
Work Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submi ...
- Mac下如何进行端口转发,方便一系列需要使用80端口进行的调试工作
上篇文章介绍到,可以在本地hosts文件中添加一条记录将微信公众号中的可信域名解析道本地127.0.0.1,但tomcat在Mac下非root权限80端口是启动不了的,所以我们可以利用pfctl端口转 ...
- matlab中help所有函数功能的英文翻译
doc funname 在帮助浏览器中打开帮助文档 help funname 在命令窗口打开帮助文档 helpbrowser 直接打开帮助浏览器 lookfor funname 搜索某个关键字相关函数 ...
- 1. Change the emulator screen size
1. "Windows" ==> "Android Virtual Device Manager" ==> Select one emulator ...
- 用Bluepages来验证intranetId和Password的有效性
代码很简单,如下: int ret = -1;ReturnCode rc = null; cwa2 cw = new cwa2();rc = cw.authenticate(in ...
- com.esotericsoftware.kryo.kryoexception java.util.ConcurentModificationException
近期 有网友看我的"整合Kafka到Spark Streaming--代码演示样例和挑战"文章, 讲 kafka对象 放到 pool 并通过broadcast广播出去: 然后 在开 ...