一天一道LeetCode系列

(一)题目

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.

(二)解题

本题想到了两个思路:暴力求解法和分治法。前者就不多说了,本文主要讨论分治法。

分治法的大致思路:对于A[low,high]这个数组,任何的连续子数组A[i,j]的位置必然是一下三种情况之一:

完全位于子数组A[low,mid]中,因此有low<=i<=j<=mid

完全位于子数组A[mid+1,high]中,因此mid


class Solution {

public:

    int maxSubArray(vector<int>& nums) {

        int len = nums.size();

        return findMaxSubArray(nums,0,len-1);

    }

    int findMaxSubArray(vector<int>& nums , int low , int high)

    {

        if(low==high) return nums[low];

        int mid = (low+high)/2;

        int left = findMaxSubArray(nums,low,mid);//子数组在左边的最大值

        int right = findMaxSubArray(nums,mid+1,high);//子数组在右边的最大值

        int cross = findMaxCrossSubArray(nums,low,high,mid);//子数组跨越中间点的时候的最大值

        if(left>=cross&&left>=right) return left;

        else if(right>=cross&&right>=left) return right;

        else return cross;//返回三个数的最大值

    }

    int findMaxCrossSubArray(vector<int>& nums , int low , int high , int mid)

    {

        int sum = 0;

        int left_sum = -2147483648;//int的最小数

        int right_sum = -2147483648;

        for(int i = mid ; i >= low ;i--)

        {

            sum+=nums[i];

            left_sum = left_sum<sum?sum:left_sum;//求左边的最大数

        }

        sum=0;

        for(int j = mid+1 ; j <= high;j++)

        {

            sum+=nums[j];

            right_sum = right_sum<sum?sum:right_sum;//求右边的最大数

        }

        return left_sum+right_sum;//返回和

    }

};

【一天一道LeetCode】#53. Maximum Subarray的更多相关文章

  1. [array] leetcode - 53. Maximum Subarray - Easy

    leetcode - 53. Maximum Subarray - Easy descrition Find the contiguous subarray within an array (cont ...

  2. 小旭讲解 LeetCode 53. Maximum Subarray 动态规划 分治策略

    原题 Given an integer array nums, find the contiguous subarray (containing at least one number) which ...

  3. 41. leetcode 53. Maximum Subarray

    53. Maximum Subarray Find the contiguous subarray within an array (containing at least one number) w ...

  4. Leetcode#53.Maximum Subarray(最大子序和)

    题目描述 给定一个序列(至少含有 1 个数),从该序列中寻找一个连续的子序列,使得子序列的和最大. 例如,给定序列 [-2,1,-3,4,-1,2,1,-5,4], 连续子序列 [4,-1,2,1] ...

  5. LN : leetcode 53 Maximum Subarray

    lc 53 Maximum Subarray 53 Maximum Subarray Find the contiguous subarray within an array (containing ...

  6. leetcode 53. Maximum Subarray 、152. Maximum Product Subarray

    53. Maximum Subarray 之前的值小于0就不加了.dp[i]表示以i结尾当前的最大和,所以需要用一个变量保存最大值. 动态规划的方法: class Solution { public: ...

  7. LeetCode 53. Maximum Subarray(最大的子数组)

    Find the contiguous subarray within an array (containing at least one number) which has the largest ...

  8. leetCode 53.Maximum Subarray (子数组的最大和) 解题思路方法

    Maximum Subarray  Find the contiguous subarray within an array (containing at least one number) whic ...

  9. [LeetCode] 53. Maximum Subarray 最大子数组

    Given an integer array nums, find the contiguous subarray (containing at least one number) which has ...

  10. C#解leetcode 53.Maximum Subarray

    Find the contiguous subarray within an array (containing at least one number) which has the largest ...

随机推荐

  1. Go 错误处理

    Go 语言通过内置的错误接口提供了非常简单的错误处理机制. error类型是一个接口类型,这是它的定义: type error interface { Error() string } 我们可以在编码 ...

  2. Go 语言递归函数

    递归,就是在运行的过程中调用自己. 语法格式如下: func recursion() { recursion() /* 函数调用自身 */ } func main() { recursion() } ...

  3. Docker 镜像

    Docker 镜像就是一个只读的模板. 例如:一个镜像可以包含一个完整的 ubuntu 操作系统环境,里面仅安装了 Apache 或用户需要的其它应用程序. 镜像可以用来创建 Docker 容器. D ...

  4. 决策树之ID3、C4.5

    决策树是一种类似于流程图的树结构,其中,每个内部节点(非树叶节点)表示一个属性上的测试,每个分枝代表该测试的一个输出,而每个树叶节点(或终端节点存放一个类标号).树的最顶层节点是根节点.下图是一个典型 ...

  5. 让你的代码量减少3倍!使用kotlin开发Android(二) --秘笈!扩展函数

    本文承接上一篇文章:让你的代码量减少3倍!使用kotlin开发Android(一) 创建Kotlin工程 本文同步自博主的私人博客wing的地方酒馆 上一节说到,kotlin可以省去getter,se ...

  6. springMVC源码分析--HttpRequestHandlerAdapter(四)

    上一篇博客springMVC源码分析--HandlerAdapter(一)中我们主要介绍了一下HandlerAdapter接口相关的内容,实现类及其在DispatcherServlet中执行的顺序,接 ...

  7. Linux目录架构详解

    Linux和Windows操作系统的显著区别之一就是目录架构的不同.Linux操作系统的目录架构遵循文件系统层级结构标准.不知你是否使用ls命令浏览过Linux的根目录"/",亲爱 ...

  8. SYBASE bcp用法及例子

    BCP是SYBASE公司提供专门用于数据库表一级数据备份的工具. 语法: 语法如下:(可用 bcp – 得到) 常用参数说明: -b batch_size 指定所复制的每批数据中的行数.每个批处理作为 ...

  9. javascript之DOM对象

    document方法 document.createElement(Tag) :创建一个html标签对象 document.getElementById(ID) :获得指定ID值的对象 documen ...

  10. 【IOS 开发】Objective-C Foundation 框架 -- 字符串 | 日期 | 对象复制 | NSArray | NSSet | NSDictionary | 谓词

    一. 字符串 API 1. NSString 用法简介 (1) NSString API 介绍 NSString 功能 : -- 创建字符串 : 使用 init 开头的实例方法, 也可以使用 Stri ...