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. 深入分析JavaWeb Item7 -- HttpServletResponse详解

    Web服务器收到客户端的http请求,会针对每一次请求,分别创建一个用于代表请求的request对象.和代表响应的response对象.request和response对象即然代表请求和响应,那我们要 ...

  2. 【BZOJ】【2219】数论之神

    中国剩余定理+原根+扩展欧几里得+BSGS 题解:http://blog.csdn.net/regina8023/article/details/44863519 新技能get√: LL Get_yu ...

  3. 混沌数学之Feigenbaum模型

          1975年,物理学家米切尔·费根鲍姆(Mitchell Feigenbaum)发现,一个可用实验加以测 量的特殊数与每个周期倍化级联相联系.这个数大约是4.669,它与π并列成为似乎在数学 ...

  4. [leetcode]Insertion Sort List @ Python

    原题地址:http://oj.leetcode.com/problems/insertion-sort-list/ 题意:对链表进行插入排序. 解题思路:首先来对插入排序有一个直观的认识,来自维基百科 ...

  5. iOS开发-Xcode入门ObjC程序

    元旦三天假跟妹子冷战一天半,剩下的半天觉得无聊,可以写点东西,折腾了下xCode 6.1,虽然iPhone6比较丑,但是不影响IOS在高端机上面的地位,ObjC是扩充C的面向对象编程语言.主要使用于M ...

  6. Pylons架构网站开发从0到1

    首先说明下这里的从0到1指的是从没有听说过pylons到开发出一个看上去还不错的网站.一个月前,我没有听说过也不知道什么是pylons,HTML只知道一些标签,JavaScript也不怎么懂,由于只倾 ...

  7. Win10系统下软件UI显示不完整解决方案

    在最初升级win10的时候就想到了这些问题,例如和各种软件的不兼容性.当然,事实上win10并没有想象的那么糟,作为一个windows user 来说,win10的确是很高大上的,无论是颜值或者是体验 ...

  8. Cognos两种建模工具对于复杂日期维度的处理比较(上)

    众所周知,在数据仓库中,日期维度是相当重要的.对数据分析的过程中可以从不同的角度去分析,比如按照下面的日期层次去分析数据. 年-季度-月-日 年-月-日 年-周-日 本示例将利用简单的商品销售分析的d ...

  9. python数据库访问

    取得rs,使用,报错 sqlite3.Cursor' object has no attribute '__getitem__' 原因:使用时conn已经关闭了. 解决:用fetchall取出传递回来 ...

  10. SqlServer日常积累(二)

    1.Like运算符:将字符串表达式与 SQL表达式中的模式进行比较匹配. 语法 :expression Like 'pattern' ,expression为匹配字段,pattern为匹配字符串.可以 ...