题目:

Given an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum.

Example:

Input: [-2,1,-3,4,-1,2,1,-5,4],
Output: 6
Explanation: [4,-1,2,1] has the largest sum = 6.

Follow up:

If you have figured out the O(n) solution, try coding another solution using the divide and conquer approach, which is more subtle.

分析:

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

我们可以将返回的结果设为INT_MIN,设一个tempsum用来记录当前和,遍历数组,将元素添加进当前和,如果当前和大于返回结果,就将返回结果更新成当前和,如果当前和小于0,我们就将当前和重置为0,因为如果当前和是负数的话,对于找最大和是没有帮助的,可以认为这段是无用的,可以舍弃。

程序:

class Solution {
public:
int maxSubArray(vector<int>& nums) {
int res = INT_MIN;
int tempSum = ;
for(auto i:nums){
tempSum += i;
if(tempSum > res) res = tempSum;
if(tempSum < ) tempSum = ;
}
return res;
}
};

LeetCode 53. Maximum Subarray最大子序和 (C++)的更多相关文章

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

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

  2. [LeetCode] 53. Maximum Subarray 最大子数组

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

  3. [leetcode]53. Maximum Subarray最大子数组和

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

  4. 53. Maximum Subarray最大子序和

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

  5. 【LeetCode】Maximum Subarray(最大子序和)

    这道题是LeetCode里的第53道题. 题目描述: 给定一个整数数组 nums ,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和. 示例: 输入: [-2,1,-3,4,-1 ...

  6. [LeetCode] 53. Maximum Subarray 最大子数组 --动态规划+分治

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

  7. [array] leetcode - 53. Maximum Subarray - Easy

    leetcode - 53. Maximum Subarray - Easy descrition Find the contiguous subarray within an array (cont ...

  8. 小旭讲解 LeetCode 53. Maximum Subarray 动态规划 分治策略

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

  9. Leetcode#53.Maximum Subarray(最大子序和)

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

随机推荐

  1. 小垃圾myl的课后实践

    #include<iostream> #include<cstdio> using namespace std; int main(){ ,flag=; printf(&quo ...

  2. ES2019 的新特性

    JavaScript 不断演变,每次迭代都会得到一些新的内部更新.让我们来看看 ES2019 有哪些新的特性,并加入到我们日常开发中 Array.prototype.flat() Array.prot ...

  3. WinCC中通过脚本禁用或启用Windows快捷键

    有些项目要求WinCC全屏运行,并禁止通过操作系统快捷键切换到桌面,这时只需要在WinCC的计算机属性中勾选“禁用用于进行操作系统访问的快捷键”.此后当WinCC运行时,按Win键或Ctrl+Alt+ ...

  4. .NET西安社区「拥抱开源,又见 .NET:壹周年Party」活动简报

    「拥抱开源,又见 .NET」:壹周年Party  .NET西安社区一岁啦!!!!7月21日,伴随着「拥抱开源,又见 .NET」系列最后一次线下分享活动暨一周年Party圆满结束, .NET西安社区一岁 ...

  5. 探索 ASP.Net Core 3.0系列三:ASP.Net Core 3.0中的Service provider validation

    前言:在本文中,我将描述ASP.NET Core 3.0中新的“validate on build”功能. 这可以用来检测您的DI service provider是否配置错误. 具体而言,该功能可检 ...

  6. VBA基础 - 分支和循环

    概要 编程语言的基础除了数据类型, 就是控制结构了. 所谓控制结构, 主要就是分支和循环. 分支 废话不说, 直接示例代码: 单条件判断 1 Sub Test() 2 If 2 > 1 Then ...

  7. vue项目使用Ueditor富文本编辑器总结

    我使用的是前端大佬封装的vue-ueditor-wrap插件,结合ueditor本身的压缩包开发的. 1.下载vue-ueditor-wrap: npm install vue-ueditor-wra ...

  8. JVM的监控工具之jconsole

    JConsole(Java Monitoring and Management Console)是一种基于JMX的可视化监视.管理工具.管理的是什么?管理的是监控信息.永久代的使用信息.类加载等等 如 ...

  9. java的递归异常—一个异常可能由另一个异常触发

    关键字: Caused by nested exception java.lang.reflect.InvocationTargetException: null at sun.reflect.Nat ...

  10. Zabbix图表中文乱码(包含Docker安装乱码)

    目录 Zabbix 4.0 版本 Zabbix 3.0 版本 Zabbix 4.0 Docker 版本 图表乱码问题解决 文章github 地址: 点我 最近在看 Zabbix 4.0 版本的官方文档 ...