这道题是LeetCode里的第53道题。

题目描述:

给定一个整数数组 nums ,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和。

示例:

输入: [-2,1,-3,4,-1,2,1,-5,4],
输出: 6
解释: 连续子数组 [4,-1,2,1] 的和最大,为 6。

进阶:

如果你已经实现复杂度为 O(n) 的解法,尝试使用更为精妙的分治法求解。

简单的动态规划题,如果之前接触过动态规划的话很容易得出答案,没有的话估计得想一阵子。

不多废话,我先介绍一下什么是动态规划吧:动态规划(Dynamic Programming)是一种分阶段求解决策问题的数学思想。总的来说就是“大事化小,小事化了”。

动态规划中包含的三个重要概念:

  1. 最优子结构
  2. 边界
  3. 状态转移方程

就拿这道题目来说,假设输入[-2,1,-3,4,-1,2,1]数组,要求其连续子数组的最大和。那它的最优子结构是什么?注意到题目要求的是连续子数组最大和。既然这样,那么求[-2,1,-3,4,-1,2,1]的连续子数组的最大和,去掉其最后一位,就可以转为求[-2,1,-3,4,-1,2]的连续子数组的最大和,因为这样可以保证它是连续的。而求[-2,1,-3,4,-1,2]的连续子数组的最大和,又是求[-2,1,-3,4,-1]的连续子数组的最大和。这样一步一步简化它。边界就是问题的范围,不可能是无穷大。这个题目的边界就是原数组的长度7。而状态转移方程呢?这是最难找的。在这里我们假设res变量保存结果即最大和,sum变量保存子结构的连续和。就拿这个例子来说。

第一步:sum<0,sum=1,sum>res→res=sum=1(sum初值为-2,res初值为0)

第二步:sum>0,sum=sum+(-3)=-2,sum<res→res=res=1

第三步:sum<0,sum=4,sum>res→res=sum=4

第四步:sum>0,sum=sum+(-1)=3,sum<res→res=res=4

第五步:sum>0,sum=sum+2=5,sum>res→res=sum=5

第六步:sum>0,sum=sum+1=6,sum>res→res=sum=6

到达边界结束

代码如下:

class Solution {
public:
int maxSubArray(vector<int>& nums) {
int res = nums[0];
int sum = 0;
for(int num : nums){
if(sum > 0)sum += num;
else sum = num;
res = res>sum?res:sum;
}
return res;
}
};

第六行的代码是一个迭代器遍历,对于数组来说,等同于

for(int i=0;i<nums.length;i++){

int num=nums[i];

......

}

运行结果:

个人总结:

第一次正式接触动态规划题目,感觉动态规划题是我的短板,需要多加练习。这次也学到了新语法。

【LeetCode】Maximum Subarray(最大子序和)的更多相关文章

  1. 【LeetCode】53. Maximum Subarray 最大子序和 解题报告(Python & C++ & Java)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力解法 动态规划 日期 题目地址: https:/ ...

  2. LeetCode 53. Maximum Subarray最大子序和 (C++)

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

  3. [LeetCode] Maximum Subarray 最大子数组

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

  4. 53. Maximum Subarray最大子序和

    网址:https://leetcode.com/problems/maximum-subarray/submissions/ 很简单的动态规划 我们可以把 dp[i] 表示为index为 i 的位置上 ...

  5. 053 Maximum Subarray 最大子序和

    给定一个序列(至少含有 1 个数),从该序列中寻找一个连续的子序列,使得子序列的和最大.例如,给定序列 [-2,1,-3,4,-1,2,1,-5,4],连续子序列 [4,-1,2,1] 的和最大,为 ...

  6. [LintCode] Maximum Subarray 最大子数组

    Given an array of integers, find a contiguous subarray which has the largest sum. Notice The subarra ...

  7. LEETCODE —— Maximum Subarray [一维DP]

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

  8. LeetCode: Maximum Subarray 解题报告

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

  9. [LeetCode]Maximum Subarray题解

    Maximum Subarray: Find the contiguous subarray within an array (containing at least one number) whic ...

随机推荐

  1. asp.net网页跳转

    1.Response.Redirect("http://www.bcbbs.net",false);    目标页面和原页面可以在2个服务器上,可输入网址或相对路径.后面的bool ...

  2. kickstart_2018_round_H_C Let Me Count The Ways

    思路: 容斥. 实现: #include <bits/stdc++.h> using namespace std; typedef long long ll; ; ; ll f[MAXN ...

  3. GUI进化--数据与界面分离

    http://blog.csdn.net/doon/article/details/5946862 1.何谓数据和界面分离? GUI,即Graphic User Interface,人机交换界面.连接 ...

  4. 看paper的网址

    http://www.arxiv-sanity.com/ https://scirate.com/ google搜cvpr open access.iccv open access

  5. java B转换KB MB GB TB PB EB ZB

    public static String readableFileSize(long size) { if (size <= 0) { return "0"; } final ...

  6. C++ _ const的用法,特别是用在函数前面与后面的区别!

    在普通的非 const成员函数中 this的类型是一个指向类类型的 const指针.可以改变this所指向的值,但不能改变 this所保存的地址. 在 const成员函数中 this的类型是一个指向 ...

  7. Caused by: java.lang.ClassNotFoundException: java.com.bj186.ssm.controller.UserController

    在搭建SpringMVC的时候,遇到的这个问题真的很奇葩, 找不到UserController这个类 这明明不就在工程目录下吗? 经过了一番艰苦卓绝的斗争, 才发现原来是包导少了 之前导入的包是: & ...

  8. Vue-Quill-Editor 富文本编辑器的使用

    步骤如下: 1.下载Vue-Quill-Editor npm install vue-quill-editor --save 2.下载quill(Vue-Quill-Editor需要依赖) npm i ...

  9. 关于回顾css发现的一些问题

    1.针对于before和after伪元素的用法: <style> .clearfix:before, .clearfix:after{ clear:both; content:" ...

  10. shell脚本,通过一个shell程序计算n的阶乘。

    [root@localhost ~]# cat jiechen.sh #!/bin/bash #设计一个shell程序计算n的阶乘,要求: #.从命令行接收参数n; #.在程序开始后立即判断n的合法性 ...