一天一道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. 苹果OS系统安装Xcode方法

    打开Xcode系统,在app store 里面找到自己系统对应的可升级的Xcode版本进行下载,下载到本地后,设置存放Xcode存放的文件夹为共享文件夹. 在MAC OS共享文件夹里面找到Xcode安 ...

  2. 用Python递归解决阿拉伯数字转为中文财务数字格式的问题(2)--打开思路的一种方法

    几天前自己写了个将阿拉伯数字转为中文财务数字的程序.用的递归,不幸的是它是树形递归. 虽然实际过程中不太可能出现金额数字大到让Python递归栈溢出,但是始终是一块心病,这玩意终究在理论上是受限制的. ...

  3. Java异常处理-----非运行时异常(受检异常)

    非运行时异常(受检异常) 如果出现了非运行时异常必须进行处理throw或者try{}catch(){}处理,否则编译器报错. 1:IOException 使用要导入包import java.io.IO ...

  4. 亲密接触Redis-第二天(Redis Sentinel)

    简介 经过上次轻松搭建了一个Redis的环境并用Java代码调通后,这次我们要来看看Redis的一些坑以及Redis2.8以后带来的一个新的特性即支持高可用特性功能的Sentinel(哨兵). Red ...

  5. SQL Server 执行计划操作符详解(1)——断言(Assert)

    前言: 很多很多地方对于语句的优化,一般比较靠谱的回复即使--把执行计划发出来看看.当然那些只看语句就说如何如何改代码,我一直都是拒绝的,因为这种算是纯蒙.根据本人经验,大量的性能问题单纯从语句来看很 ...

  6. 即时通讯软件openfire+spark+smack

    所以我基本上分为三篇文章来介绍此类软件的开发: 第一篇是关于XMPP 协议是啥,IM 是啥以及一个比较有名的开源实现,该开源实现包括三个部分(Spark.Smack和Openfire): 第二篇讲如何 ...

  7. 7.0、Android Studio命令行工具

    命令行工具分成SDK工具和平台工具. SDK工具 SDK工具跟随SDK安装包安装并随时更新. Virtual Device 工具 1. Android Virtual Device Manager 提 ...

  8. 2.4、Android Studio使用主题编辑器设计主题

    Android Studio包含一个叫主题编辑器的可视的助手,可以提供以下功能: 1. 创建和更改你的app主题 2. 为不同的资源适应主题 3. 普通的UI颜色更改的实时显示 主题编辑器 这一节描述 ...

  9. 1079. Total Sales of Supply Chain (25) -记录层的BFS改进

    题目如下: A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)-- everyon ...

  10. Cocos2D瓦块地图高清屏(retina)显示比例问题的解决

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;) 在Cocos2D的游戏编程里,常用到瓦块地图.而cocos2D ...