一天一道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. Check the string CodeForces - 960A

    A has a string consisting of some number of lowercase English letters 'a'. He gives it to his friend ...

  2. 使用POI实现报表打印功能

    [版权申明:本文系作者原创,转载请注明出处] 文章出处:http://blog.csdn.net/sdksdk0/article/details/53393453 作者:朱培 ID:sdksdk0 这 ...

  3. [Gradle系列]Gradle打包apk多版本,多渠道,多环境,多功能,多模块随心所欲

    Tamic: http://blog.csdn.net/sk719887916/article/details/53411771 开始 上篇Gradle发布Module(Maven)到jcenter, ...

  4. OpenCV + Python 人脸检测

    必备知识 Haar-like opencv api 读取图片 灰度转换 画图 显示图像 获取人脸识别训练数据 探测人脸 处理人脸探测的结果 实例 图片素材 人脸检测代码 人脸检测结果 总结 下午的时候 ...

  5. Compass实战 站内搜索

    今天早上打算对这两天学习的Lucene以及Compass总结一下,想来想去,还是写个小项目来验证最好了.于是就有了今天的这篇文章.难易程度适合对于Compass或者Lucene刚入门的童鞋,大牛看到后 ...

  6. Linux telnet远程登录操作

    telnet  (如果不行 可以却换root帐户试试 su - root) 1.安装telnet-server     sudo dpkg -i xinetd_1%3a2.3.14-7ubuntu3_ ...

  7. shell test和find命令实例解析

    shell test和find命令实例解析 下面以\build\core\product.mk相关部分来学习 define _find-android-products-files $(shell t ...

  8. [ExtJS5学习笔记]第三十二节 sencha extjs 5与struts2的ajax交互配置

    本文地址:http://blog.csdn.net/sushengmiyan/article/details/43487751 本文作者:sushengmiyan ------------------ ...

  9. [代码应用]javaSE程序递归删除文件夹下的.bak文件程序源代码

    本文地址:http://blog.csdn.net/sushengmiyan/article/details/39158939 本文作者:sushengmiyan ------------------ ...

  10. 用API创建用户

    DECLARE lc_user_name VARCHAR2(100) := 'PRAJ_TEST'; lc_user_password VARCHAR2(100) := 'Oracle123'; ld ...