题目

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)的算法,属于简单的动态规划,根据题目后面的more practice说明该题目还有更优的分治法解决思路。

AC代码-动态规划

class Solution {
public:
int maxSubArray(vector<int>& nums) { if (nums.empty())
return 0; //求数组的长度
int len = nums.size(); //将最大和赋值为首元素值,temp记录临时子序列和
int maxSum = nums[0], temp = 0;
for (int i = 0; i < len; i++)
{
temp += nums[i]; //若元素和大于当前最大和
if(temp > maxSum)
{
maxSum = temp;
}//else //若子系列和为非正数,则从下一个元素重新记录
if (temp <= 0)
{
temp = 0;
} }//for return maxSum;
}
};

AC代码-分治法

class Solution {
public:
int maxSubArray(vector<int>& nums) { if (nums.empty())
return 0; //求数组的长度
int len = nums.size(); return Divide(nums , 0 , len-1);
} //分治法
int Divide(const vector<int> &nums, int lhs, int rhs)
{
if (lhs == rhs)
return nums[lhs]; int mid = (lhs + rhs) / 2;
int leftMaxSum = Divide(nums, lhs, mid);
int rightMaxSum = Divide(nums, mid + 1, rhs); int lsum = INT_MIN;
int rsum = INT_MIN; int temp = 0;
for (int i = mid; i >= lhs; i--)
{
temp += nums[i];
if (temp > lsum)
lsum = temp;
} temp = 0;
for (int i = mid + 1; i <= rhs; i++)
{
temp += nums[i];
if (temp > rsum)
rsum = temp;
} //跨越中点的最大子序列和
temp = lsum + rsum; return std::max(temp, std::max(leftMaxSum, rightMaxSum));
}
};

GitHub测试程序源码

LeetCode(53) Maximum Subarray的更多相关文章

  1. LeetCode(152) Maximum Product Subarray

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

  2. Leetcode(53)-最大子序和

    给定一个整数数组 nums ,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和. 示例: 输入: [-2,1,-3,4,-1,2,1,-5,4], 输出: 6 解释: 连续子数组 ...

  3. (LeetCode 53)Maximum Subarray

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

  4. [LeetCode]题53:Maximum Subarray

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

  5. LeetCode(53):最大子序和

    Easy! 题目描述: 给定一个整数数组 nums ,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和. 示例: 输入: [-2,1,-3,4,-1,2,1,-5,4], 输出: ...

  6. LeetCode(164)Maximum Gap

    题目 Given an unsorted array, find the maximum difference between the successive elements in its sorte ...

  7. LeetCode(104) Maximum Depth of Binary Tree

    题目 Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the l ...

  8. 【LeetCode算法-53】Maximum Subarray

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

  9. LeetCode(122) Best Time to Buy and Sell Stock II

    题目 Say you have an array for which the ith element is the price of a given stock on day i. Design an ...

随机推荐

  1. CMake学习笔记一:初识cmake

    1 cmake简介 1.1 背景知识 cmake 是 kitware 公司以及一些开源开发者在开发几个工具套件(VTK)的过程中衍生品,最终形成体系,成为一个独立的开放源代码项目.项目的诞生时间是 2 ...

  2. [BZOJ2056]gift? 高精度?

    Description Input 输入的第一行为一个整数t. 接下来t行,每行包含九个自然数. Output 输出t行 每行一个整数,表示\(2^a+2^b+2^c+2^d+2^e+2^f+2^g+ ...

  3. 解题报告:hdu 1556 Color the ball(区间修改,单点查询)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1556 Problem Description N个气球排成一排,从左到右依次编号为1,2,3....N ...

  4. MyEclipse去除不必要的validation

    MyEclipse在构建项目时去除不必要的Valication可以加快构建速度. 操作: Window->Perferences->MyEclipse->Validation 在Va ...

  5. android将对象序列化到文件:直接写文件与用Serializable接口的对比

    1.用文件读写1024个对象的日志 10-09 16:12:44.493 6385-6385/com.example.tt.downtest D/Serializable_TAG: write 102 ...

  6. [转]Using the Repository Pattern with ASP.NET MVC and Entity Framework

    本文转自:http://www.codeguru.com/csharp/.net/net_asp/mvc/using-the-repository-pattern-with-asp.net-mvc-a ...

  7. CSS3 按钮特效(一)

    1. 实例 2.HTML 代码 <!DOCTYPE html> <html lang="en"> <head> <meta charset ...

  8. iOS/Android 视频编辑SDK

    锐动天地为开发者提供短视频编辑.特效.直播.录屏.编解码.视频转换,等多种解决方案,涵盖PC.iOS.Android多平台.以市场为导向,不断打磨并创新技术,在稳定性,兼容性,硬件设备效率优化上千捶百 ...

  9. .Net Mvc EasyUI DataGrid 分页

    由于项目的需要,最近一直在学习 .net MVC 和EasyUI.上周写了一个<.Net Mvc 返回Json,动态生成EasyUI Tree>,今天再写一个EasyUI中另一个重要的组件 ...

  10. H3C AR28-31路由器组网实验

    接线图 可以发现PC1和PC2不在一个网段上,如果不靠路由器就不可能ping,所以要用路由器组网 接线步骤 串行线连接路由器1与路由器2 以太网线连路由器以太网口 与 交换机接口 计算机网线连交换机口 ...