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 ...
随机推荐
- spring源代码下载并导入eclipse技巧
环境:mac 安装brew /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install ...
- 标准字符cp功能
#include<stdio.h> #include<fcntl.h> int main(int argc,char *argv[]) { FILE *src_fp,*des_ ...
- cenos mkdir 无法创建文件夹,即便文件权限为777
SELinux 拒绝了httpd的方式去读写此目录 chcon -R -t httpd_sys_content_rw_t /var/www/html
- SpringCloud开发学习总结(六)—— 结合注解的AOP示例
面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术.AOP是OOP的延续,是软件开发中的一个热点,也是Spring框架中的一个重要内容,是函数式编程的一种衍生范型.利用AOP ...
- sed附加命令
追加命令(命令a) sed '[address] a the-line-to-append' input-file 在第二行后面追加一行(原文这里可能有问题,没有写名行号) [root@sishen ...
- API系列一:REST和RESTful认识
序言 最近工作的项目一直使用API,就想趁這个机会,把API的知识点进行一次梳理和总结,顺便提升一下自己对API全新的认识 Web API 是ASP.NET平台新加的一个特性,它可以简单快速地创建We ...
- vue.js学习参考手册
参考手册 示例:www.51siyuan.cn/161.html
- 一个net程序猿必备工具
自古以来,人类的进步都是依赖于工具的进步,从刀耕火种,到使用青铜器,再到现在的科技,每一次都使我们的工作效率提高了无数倍,所以一个好的工具能使我们提高无数倍的工作效率,下面,我就根据自己简单的总结一下 ...
- AJPFX总结IO流中的缓冲思想
缓冲思想 (因为内存的运算速度要远大于硬盘的原酸速度,所以只要降低硬盘的读写次数,就可以提高效率) 1. 字节流一次读写一个数组的速度明显比一次读写一个字节的速度快很多, 2. 这是加 ...
- Spring------IOC&DI
一.Spring? Spring兴起:2003年,由Rod Johnson创建.总的来说,Spring Framwork从它诞生至今都一直为人所称道,它的伟大之处自此可见一斑. 核心:IOC& ...