【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 ...
随机推荐
- HDU 4864 Task(贪心)
HDU 4864 Task 题目链接 题意:有一些机器和一些任务.都有时间和等级,机器能做任务的条件为时间等级都大于等于任务.而且一个任务仅仅能被一个机器做.如今求最大能完毕任务.而且保证金钱尽量多 ...
- 深入理解Java中的组合和继承
Java是一个面向对象的语言.每一个学习过Java的人都知道,封装.继承.多态是面向对象的三个特征.每个人在刚刚学习继承的时候都会或多或少的有这样一个印象:继承可以帮助我实现类的复用.所以,很多开发人 ...
- Objective-C:NSDectionary字典的常见操作
NSDectionary字典:它是一个存储键值的容器,每一个键key都对应着一个值value,可以通过键key一次性找到目标值value,这是一个比较好的存储器,相比于数组而言,它明显提高了查询效率. ...
- 【转】QT CEF3 消息循环处理
初次写博客,可能有点乱, 按照自己的实际经历谈一下CEF3钟遇到的一些坑,希望对以后的小伙有些帮助. 先说一下经历,当初第一次接触CEF3的时候,没做特殊处理,直接将cef3封装成控件,嵌入到QT程序 ...
- is-subsequence
public class Solution { public boolean isSubsequence(String s, String t) { int idx = 0; for (int i=0 ...
- asm rebalance 原理
详见原文博客链接地址: asm rebalance 原理
- Java HashMap 默认排序
先看一段Java代码. package com.m58.test; import java.text.ParseException; import java.text.SimpleDateFormat ...
- 【Java VisualVM】使用 VisualVM 进行性能分析及调优
转载:https://blog.csdn.net/lmb55/article/details/79267277 一.概述 开发大型 Java 应用程序的过程中难免遇到内存泄露.性能瓶颈等问题,比如文件 ...
- 多个桌面Deskspace如何使用
1 给Deskspace设置背景.在DeskSpace选项中设置显示背景为天空箱体图像(软件自带的图像效果,也可以使用静态图像,即自己的图片) 2 给六个桌面各设置一个背景(也可以使用同一个背景)右击 ...
- ArcGIS安装问题集锦
1. 软件安装 软件下载.安装问题自行解决,否则就不要使用. 2. 常见问题 2.1 许可管理器版本不正确 2013年3月19日 问题一:ArcGIS10安装后,更改许可管理器时,通常,在ArcGIS ...