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.

click to show more practice.

More practice:

If you have figured out the O(n) solution, try coding another solution using the divide and conquer approach, which is more subtle.

这道题让我们求最大子数组之和,并且要我们用两种方法来解,分别是O(n)的解法,还有用分治法Divide and Conquer Approach,这个解法的时间复杂度是O(nlgn),那我们就先来看O(n)的解法,定义两个变量res和curSum,其中res保存最终要返回的结果,即最大的子数组之和,curSum初始值为0,每遍历一个数字num,比较curSum + num和num中的较大值存入curSum,然后再把res和curSum中的较大值存入res,以此类推直到遍历完整个数组,可得到最大子数组的值存在res中,代码如下:

C++ 解法一:

class Solution {
public:int maxSubArray(vector<int>& nums) {int res = INT_MIN, curSum = 0;for (int num : nums) {curSum = max(curSum + num, num);res = max(res, curSum);}return res;}
};

Java 解法一:

public class Solution {public int maxSubArray(int[] nums) {int res = Integer.MIN_VALUE, curSum = 0;for (int num : nums) {curSum = Math.max(curSum + num, num);res = Math.max(res, curSum);}return res;}
}

题目还要求我们用分治法Divide and Conquer Approach来解,这个分治法的思想就类似于二分搜索法,我们需要把数组一分为二,分别找出左边和右边的最大子数组之和,然后还要从中间开始向左右分别扫描,求出的最大值分别和左右两边得出的最大值相比较取最大的那一个,代码如下:

C++ 解法二:

class Solution {
public:int maxSubArray(vector<int>& nums) {if (nums.empty()) return 0;return helper(nums, 0, (int)nums.size() - 1);}int helper(vector<int>& nums, int left, int right) {if (left >= right) return nums[left];int mid = left + (right - left) / 2;int lmax = helper(nums, left, mid - 1);int rmax = helper(nums, mid + 1, right);int mmax = nums[mid], t = mmax;for (int i = mid - 1; i >= left; --i) {t += nums[i];mmax = max(mmax, t);}t = mmax;for (int i = mid + 1; i <= right; ++i) {t += nums[i];mmax = max(mmax, t);}return max(mmax, max(lmax, rmax));}
};

Java 解法二:

public class Solution {public int maxSubArray(int[] nums) {if (nums.length == 0) return 0;return helper(nums, 0, nums.length - 1);}public int helper(int[] nums, int left, int right) {if (left >= right) return nums[left];int mid = left + (right - left) / 2;int lmax = helper(nums, left, mid - 1);int rmax = helper(nums, mid + 1, right);int mmax = nums[mid], t = mmax;for (int i = mid - 1; i >= left; --i) {t += nums[i];mmax = Math.max(mmax, t);}t = mmax;for (int i = mid + 1; i <= right; ++i) {t += nums[i];mmax = Math.max(mmax, t);}return Math.max(mmax, Math.max(lmax, rmax));}
}

本文转自博客园Grandyang的博客,原文链接:最大子数组[LeetCode] Maximum Subarray ,如需转载请自行联系原博主。

[LeetCode] Maximum Subarray 最大子数组相关推荐

  1. [LintCode] Maximum Subarray 最大子数组

    Given an array of integers, find a contiguous subarray which has the largest sum. Notice The subarra ...

  2. LeetCode Maximum Product Subarray(最大子数组乘积)

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

  3. LeetCode Maximum Subarray

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

  4. LeetCode: Maximum Subarray 解题报告

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

  5. [LeetCode] Maximum Subarray

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

  6. LeetCode - Maximum Subarray

    题目: Find the contiguous subarray within an array (containing at least one number) which has the larg ...

  7. leetcode系列-53.最大子数组和

    题目描述: 给你一个整数数组 nums ,请你找出一个具有最大和的连续子数组(子数组最少包含一个元素), 返回其最大和.子数组 是数组中的一个连续部分. 示例 1: 输入:nums = [-2,1,- ...

  8. 【LeetCode】LeetCode之乘积最大子数组——枚举+动态规划+Kadane算法

  9. LeetCode: Maximum Subarray

    自己想得太复杂了,网上查了别人的代码..太简单了.. 1 class Solution { 2 public: 3 int maxSubArray(int A[], int n) { 4 // Sta ...

最新文章

  1. 斯坦福公开课 密码学 cryptography 1 思维导图
  2. php smeoa,install.php
  3. pycharm导入jieba包_3分钟带你搞懂Python模块、包的区别和使用
  4. altium designer 10哪个作者写的好 。
  5. css 背景图怎么设置自动填充满_CSS属性设置 -- 背景样式
  6. 【OpenGL】详解第一个OpenGL程序
  7. Codeforces Round #350 (Div. 2) B. Game of Robots 水题
  8. (9)vivado ila IP使用示例(学无止境)
  9. ETL__pentaho__SPOON_PDI
  10. java 静态缓存_JAVA缓存的实现
  11. double除以int结果是int吗_游戏开发java中int可以用汉字吗?
  12. 【Flutter】基础组件【01】Text
  13. 数理逻辑习题集(6)
  14. 将BC26连接至OneNET平台
  15. flyway 社区版本使用团队(企业级)特性ignore-migration-patterns使用
  16. 友情链接加nofollow_如何在WordPress中添加Nofollow链接(适用于初学者的简单指南)
  17. 出海季,互联网出海锦囊之本地化
  18. 2021-05-21 qt程序aas运行失败提示undefined symbol: _Z34QBasicAtomicInt_fetchAndAddOrderedPVii问题
  19. 通过adb操作安卓亮屏、设置背光亮度、解锁、打开app
  20. UDS之浅谈11服务

热门文章

  1. 大脑模拟NLP,高德纳奖得主:神经元集合演算用于句子解析
  2. SAP MM 如何看一个Inbound Delivery单据相关的IDoc?
  3. 重读图灵经典之作,九条反驳意见引人深思
  4. SAP S/4HANA BP功能
  5. 风之语.我看苏州511房产新政
  6. 为基于树的机器学习模型构建更好的建模数据集的10个小技巧!
  7. 人工智能项目的六投三不投
  8. 深度学习神经网络都是从CNN和AlexNet开始的
  9. 2019需要关注的几大AI趋势
  10. 推荐算法——基于协同过滤CF