一天一道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. Node.js 集群

    稳定性: 2 - 不稳定 单个 Node 实例运行在一个线程中.为了更好的利用多核系统的能力,可以启动 Node 集群来处理负载. 在集群模块里很容易就能创建一个共享所有服务器接口的进程. var c ...

  2. Android对话框Dialog深度剖析

    对话框 对话框是提示用户作出决定或输入额外信息的小窗口. 对话框不会填充屏幕,通常用于需要用户采取行动才能继续执行的模式事件. 对话框设计 Dialog 类是对话框的基类,但您应该避免直接实例化 Di ...

  3. 剑指Offer——企业级项目中分层的含义与依据及多态的优势

    剑指Offer--企业级项目中分层的含义与依据及多态的优势   关于以上两点,由于项目经验较少,自己不是很明白,特整理如下. 常见分层架构模式 三层架构 3-tier architecture   微 ...

  4. EBS业务学习之应付管理

    应付款系统是供应链管理的最后一个环节,它使公司能够支付供应商提供的货物和服务的费用.供应链管理的目标是保持低库存量但又有充足的存货以满足要求,仓库中的库存就等于钱,因此,应付款管理的目标是尽可能地推迟 ...

  5. Android应用UI设计流程

    Android应用UI设计流程 设计原理 1.在移动设计中,使用环境是最关键的因素.原型设计方法必须考虑尺寸因素 2.用户测试必须涵盖运动.声音和多点触控等方面: 进行移动设计和测试时,请将你知道的有 ...

  6. 第三方开源动画库EasyAnimation中一个小bug的修复

    看过iOS动画之旅的都知道,其中在最后提到一个作者写的开源动画库EasyAnimation(以下简称EA). EA对CoreAnimation中的view和layer动画做了更高层次的包装和抽象,使得 ...

  7. 六星经典CSAPP笔记(1)计算机系统巡游

    CSAPP即<Computer System: A Programmer Perspective>的简称,中文名为<深入理解计算机系统>.相信很多程序员都拜读过,之前买的旧版没 ...

  8. hive分组排序 取top N

    pig可以轻松获取TOP n.书上有例子 hive中比较麻烦,没有直接实现的函数,可以写udf实现.还有个比较简单的实现方法: 用row_number,生成排名序列号.然后外部分组后按这个序列号多虑, ...

  9. 自己写一个网页版的Markdown实时编辑器

    这几天忙着使用Python+Django+sqlite 搭建自己的博客系统,但是单纯的使用H5的TextArea,简直太挫了有木有.所以,就想模仿一下人家内嵌到网页上的Markdown编辑器,从而让自 ...

  10. SYBASE bcp用法及例子

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