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. strcmp实现

    #include<stdio.h> #include<assert.h> int my_strcmp(const char*a,const char*b) { while(*a ...

  2. Javascript模块化工具require.js教程

    转自:http://www.w3cschool.cc/w3cnote/requirejs-tutorial-1.html, http://www.w3cschool.cc/w3cnote/requir ...

  3. 利用Python爬虫爬取指定天猫店铺全店商品信息

    本编博客是关于爬取天猫店铺中指定店铺的所有商品基础信息的爬虫,爬虫运行只需要输入相应店铺的域名名称即可,信息将以csv表格的形式保存,可以单店爬取也可以增加一个循环进行同时爬取. 源码展示 首先还是完 ...

  4. jQuery多文件下载

    文件下载是一个Web中非常常用的功能,不过你是做内部管理系统还是做面向公众的互联网公司都会遇到这个问题,对于下载一般有点实际开发经验的都会自己解决,上周弄了一下多文件下载,业务场景就是一条数据详细信息 ...

  5. Unable to find manifest signing certificate in the certificate store

    方法一:把DEF项目的属性->Signing选项->Sign the ClickOnce manifests 勾去掉,这样就可以编绎通过了: 方法二:用记事本打开 *.csproj文件 , ...

  6. OA系统启动:基础数据,工作流设计

    自从开源OA系统启动:系统概览放 出来后.园友们反馈了一些不错的建议.主要集中在工作流部分.本来是先不考虑工作流部分.这些天的交流和思考.决定把工作流部分作为系统基础结构贯穿整个 系统.所以先考虑了这 ...

  7. C# 中使用 RSA加解密算法

    一.什么是RSA RSA公开密钥密码体制.所谓的公开密钥密码体制就是使用不同的加密密钥与解密密钥,是一种“由已知加密密钥推导出解密密钥在计算上是不可行的”密码体制. 在公开密钥密码体制中,加密密钥(即 ...

  8. 架构师书单 2nd Edition

    了2007年的目标,列了下面待读或重读的书单.    "其实中国程序员,现在最需要的是一张安静的书桌.",的确,中国架构师大多缺乏系统的基础知识,与其自欺欺人的宣扬"读书 ...

  9. j2ee model1模型完成分页逻辑的实现 详解!

    在显示用户全部信息的页面,在显示全部数据的时候,长长的滚动条,像是没有边界的天空一样, 让用户查看数据很不方便. 于是, 我们要把这些数据分页显示, 就像office的word一样,每页显示一定数量的 ...

  10. SQL Server-已更新或删除的行值要么不能使该行成为唯一行,要么改变了多个行

    在更新没有设置主键的表的时候出现下图中的问题: 问题原因: 这种问题大多是由于没有主键(PK)导致同一张表中存在若干条相同的数据 DBMS存储时,只为其存储一条数据,因为DBMS底层做了优化,以减少数 ...