LeetCode(53) Maximum Subarray
题目
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));
}
};
LeetCode(53) Maximum Subarray的更多相关文章
- LeetCode(152) Maximum Product Subarray
题目 Find the contiguous subarray within an array (containing at least one number) which has the large ...
- Leetcode(53)-最大子序和
给定一个整数数组 nums ,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和. 示例: 输入: [-2,1,-3,4,-1,2,1,-5,4], 输出: 6 解释: 连续子数组 ...
- (LeetCode 53)Maximum Subarray
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- [LeetCode]题53:Maximum Subarray
Given an integer array nums, find the contiguous subarray (containing at least one number) which has ...
- LeetCode(53):最大子序和
Easy! 题目描述: 给定一个整数数组 nums ,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和. 示例: 输入: [-2,1,-3,4,-1,2,1,-5,4], 输出: ...
- LeetCode(164)Maximum Gap
题目 Given an unsorted array, find the maximum difference between the successive elements in its sorte ...
- 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 ...
- 【LeetCode算法-53】Maximum Subarray
Given an integer array nums, find the contiguous subarray (containing at least one number) which has ...
- 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 ...
随机推荐
- Python爬虫库-Beautiful Soup的使用
Beautiful Soup 是一个可以从HTML或XML文件中提取数据的Python库,简单来说,它能将HTML的标签文件解析成树形结构,然后方便地获取到指定标签的对应属性. 如在上一篇文章通过爬虫 ...
- urllib的高级用法
Handler简介 我们可以把他理解为各种处理器,有专门处理登录验证的,有处理cookies的,有处理代理设置的.利用他们,我们几乎可以做到HTTP请求中的所有事情. 首先,介绍一下 urllib.r ...
- Android推送服务(2)微信智能心跳方案
http://mp.weixin.qq.com/s?__biz=MzAwNDY1ODY2OQ==&mid=207243549&idx=1&sn=4ebe4beb8123f1b5 ...
- jQuery委托
$('#container').on('click', '.elementClass', function() { // code }); http://stackoverflow.com/quest ...
- 【转】qqface使用实例
原网址:http://www.xwcms.net/js/bddm/51565.html <div id="show"></div> <div cl ...
- 冒泡排序算法和简单选择排序算法的js实现
之前已经介绍过冒泡排序算法和简单选择排序算法和原理,现在有Js实现. 冒泡排序算法 let dat=[5, 8, 10, 3, 2, 18, 17, 9]; function bubbleSort(d ...
- 对象输入输出流ObjectInputStream、ObjectOutputStream(对象序列化与反序列化)
对象的输入输出流 : 主要的作用是用于写入对象信息与读取对象信息. 对象信息一旦写到文件上那么对象的信息就可以做到持久化了 对象的输出流: ObjectOutputStream 对象的输入流: Ob ...
- Asp.Net 设计模式 之 “特殊”的单例模式
特殊的单例模式 要点在这里,提前预览: public SingleDemo() { name = "yy"; age = 20; //特殊的单例,this指代得失当前的Single ...
- 【译】x86程序员手册41-10.6 TLB(快表)测试
译注:本章基本未做翻译 10.6 TLB Testing TLB测试 The 80386 provides a mechanism for testing the Translation Lookas ...
- ARM开发板如何选型-I.MX6Q开发板
拥有丰富扩展能力,供货周期长的开发平台,省事安心 处理器:迅为-i.MX6开发板恩智浦Cortex-A9 四核i.MX6Q处理器,主频1GHz,内存2G,存储16GB. 系统支持:i.MX6开发板 ...