题目

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. Luogu P1144 最短路计数 【最短路】 By cellur925

    题目传送门 常规的最短路计数问题:注意有重边(重边不用理,看样例),自环(读入时过滤). 另外这个无向图没有权,其实可以直接bfs做,但考虑到以后带权的情况,按spfa走了. 水题被卡了三次(嘤嘤嘤 ...

  2. POJ 2234 Nim博弈

    思路: nim博弈裸题 xor一下 //By SiriusRen #include <cstdio> using namespace std; int n,tmp,xx; int main ...

  3. 一步一步学习GTK

    完成课设,偶然碰到GTK,索性学习一遍 转载自ikoDotA の BLOG (一).一步一步学GTK+之开篇 一.什么是GTK+ GTK+ 是一种函数库是用来帮助制作图形交互界面的.整个函数库都是由C ...

  4. 构造 Codeforces Round #310 (Div. 2) B. Case of Fake Numbers

    题目传送门 /* 题意:n个数字转盘,刚开始每个转盘指向一个数字(0~n-1,逆时针排序),然后每一次转动,奇数的+1,偶数的-1,问多少次使第i个数字转盘指向i-1 构造:先求出使第1个指向0要多少 ...

  5. 题解报告:hdu 2546 饭卡(01背包)

    Problem Description 电子科大本部食堂的饭卡有一种很诡异的设计,即在购买之前判断余额.如果购买一个商品之前,卡上的剩余金额大于或等于5元,就一定可以购买成功(即使购买后卡上余额为负) ...

  6. ASP.NET CORE 使用 EF CORE访问数据库

    asp.net core通过ef core来访问数据库,这里用的是代码优先,通过迁移来同步数据库与模型. 环境:vs2017,win10,asp.net core 2.1 一.从建立asp.net c ...

  7. AJPFX讲解java单例模式

    单例设计模式概述:      单例模式就是要确保类在内存中只有一个对象,该实例必须自动创建,并且对外提供单例模式有以下特点: 1.单例类只能有一个实例. 2.单例类必须自己自己创建自己的唯一实例. 3 ...

  8. (二)Spring容器

    大佬总结的很好,请去看大佬博客. http://www.cnblogs.com/chenssy/archive/2012/11/15/2772287.html https://www.cnblogs. ...

  9. Java基础教程(24)--集合

    一.Java集合框架   集合,有时也称为容器,是一个用来存储和管理多个元素的对象.Java中的集合框架定义了一套规范,用来表示和操作集合,使具体操作与实现细节解耦.集合框架都包含下列内容: 接口:这 ...

  10. Vue 学习之el、template、replace和vue的生命周期 参考网址:https://segmentfault.com/a/1190000008010666