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)的更多相关文章

  1. 【LeetCode】53. Maximum Subarray 最大子序和 解题报告(Python & C++ & Java)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力解法 动态规划 日期 题目地址: https:/ ...

  2. 【Leetcode】53. Maximum Subarray

    题目地址: https://leetcode.com/problems/maximum-subarray/description/ 题目描述: 经典的求最大连续子数组之和. 解法: 遍历这个vecto ...

  3. 【LeetCode】053. Maximum Subarray

    题目: Find the contiguous subarray within an array (containing at least one number) which has the larg ...

  4. 【一天一道LeetCode】#53. Maximum Subarray

    一天一道LeetCode系列 (一)题目 Find the contiguous subarray within an array (containing at least one number) w ...

  5. 【leetcode】1186. Maximum Subarray Sum with One Deletion

    题目如下: Given an array of integers, return the maximum sum for a non-empty subarray (contiguous elemen ...

  6. 【LeetCode】164. Maximum Gap (2 solutions)

    Maximum Gap Given an unsorted array, find the maximum difference between the successive elements in ...

  7. 【LeetCode】718. Maximum Length of Repeated Subarray 解题报告(Python)

    [LeetCode]718. Maximum Length of Repeated Subarray 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxu ...

  8. 【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 ...

  9. 【LeetCode】895. Maximum Frequency Stack 解题报告(Python)

    [LeetCode]895. Maximum Frequency Stack 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxueming ...

随机推荐

  1. Redis中对Key进行分类

    使用":"体现层次 >set key1:key2:key4 value1 "OK" >set key1:key2:key5 value2 " ...

  2. STL队列 之FIFO队列(queue)、优先队列(priority_queue)、双端队列(deque)

    1.FIFO队列   std::queue就是普通意思上的FIFO队列在STL中的模版. 1.1主要的方法有: (1)T front():访问队列的对头元素,并不删除对头元素 (2)T back(): ...

  3. 10 款基于 jQuery 的切换效果插件推荐

    本文整理了 10 款非常好用的 jQuery 切换效果插件,包括平滑切换和重叠动画等,这些插件可以实现不同元素之间的动态切换. 1. InnerFade 这是一个基于 jQuery 的小插件,可以实现 ...

  4. Oracle中的数值处理方法

    求绝对值函数 ) from dual; 求平方根函数 ) from dual; 求幂函数 ,3) from dual; 求余弦三角函数 select cos(3.14159) from dual; 求 ...

  5. 将Tp-link无线路由器桥接到Dlink无线路由器上

    笔者家中原有两台笔记本和两台IPad,通过一台Dlink无线路由器(型号DIR-612,以下简称Dlink)上网,Dlink以PPPOE方式连到小区宽带.一直还可以. 后来为了练习Linux,启用了一 ...

  6. C++ Linux 多线程之创建、管理线程

    线程就是,在同一程序同一时间内同意运行不同函数的离散处理队列. 这使得一个长时间去进行某种特殊运算的函数在运行时不阻碍其它的函数变得十分重要. 线程实际上同意同一时候运行两种函数,而这两个函数不必相互 ...

  7. Linux清理磁盘空间

    1.首先确定是否是磁盘满了 命令:   df -h 参数说明: -a:列出所有的文件系统,包括系统特有的/proc等文件系统 -k:以KB的容量显示各文件系统 -m:以MB的容量显示各文件系统 -h: ...

  8. Octave中plot函数的用法

    octave:14> help plot'plot' is a function from the file C:\Octave\Octave3.6.4_gcc4.6.2\share\octav ...

  9. MySQL Cluster线上管理节点配置文件-数据节点32G内存

    网上的朋友提供的,仅供参考. [NDBD DEFAULT] NoOfReplicas= 2 DataMemory=20G IndexMemory=5G MaxNoOfConcurrentTransac ...

  10. Centos6.6系统root用户密码恢复案例(转)

    原文:http://www.centoscn.com/CentOS/Intermediate/2015/0131/4604.html 通过单用户模式恢复root用户密码 重新启动主机后,在出现Grub ...