题目

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. POJ2486 Apple Tree 【树上背包】

    一句话题意:一棵树,一共n个点,每个点上有一个权值,求从1出发,走k步,最多能遍历到的权值.可以往回走. 第一(二)道树上背包题,先是看了dalao的题解,改了一点就过样例了.然而....TLE??? ...

  2. SpringMVC传递multiple类型select后台Controller的接收方法

    Spring MVC在接收集合请求参数时,需要在Controller方法的集合参数里前添加@RequestBody,而@RequestBody默认接收的enctype (MIME编码)是applica ...

  3. Coins HDU - 2844 POJ - 1742

    Coins HDU - 2844 POJ - 1742 多重背包可行性 当做一般多重背包,二进制优化 #include<cstdio> #include<cstring> in ...

  4. java数组实现买彩票(重复则重新遍历查询思想)

    package com.wh.shuzu; import java.util.Arrays; /** * 买彩票 * @author 丁璐同学 * 重复则重新遍历查询思想 */ public clas ...

  5. 416 Partition Equal Subset Sum 分割相同子集和

    详见:https://leetcode.com/problems/partition-equal-subset-sum/description/ C++: class Solution { publi ...

  6. 1270 数组的最大代价 dp

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1270&judgeId=194704 一开始贪心,以为就两种情况, ...

  7. mysql各个版本下载地址

    之所记录下来是因为我找了好久才找到:这下记着了:http://downloads.mysql.com/archives/community/ 希望对没有找到的朋友有帮助

  8. SpringSecurity的简单使用

    导入SpringSecurity坐标 在web.xml中配置过滤器 编写spring-securiy配置文件 编写自定义认证提供者 用户新增时加密密码 配置页面的login和logout 获取登录用户 ...

  9. Sqlmap脱库之“你的数据我所见”

    废话不多说,介绍和理论啥的网上一搜一大把,一次只为tuoku. 实战操作: 1.在网上找个index.php?id=1的站点 2.很明显看到了脱下裤子的希望,sqlmap走一把.查看数据库 3.怎么看 ...

  10. SELECT TOP 100 PERCENT * 的含义

    --返回符合条件的100%的记录,即所有符合条件的记录SELECT TOP 100 PERCENT * --返回符合条件的100条记录,即只返回符合条件的100条记录SELECT TOP 100 * ...