题目:

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. hw笔试题-01

    #include <stdlib.h> #include <stdio.h> #include <string.h> int str_split(char *inp ...

  2. B1013(通过)

    这种方法是采用B1017的那个求素数的算法,并且送一个比较大的数值当作上线(20000),也可以进一步压缩,但是这个数已经够用了,就没有再试了. python方便是方便,但是真的慢 def isPri ...

  3. [NewLife.XCode]角色权限

    NewLife.XCode是一个有10多年历史的开源数据中间件,支持nfx/netcore,由新生命团队(2002~2019)开发完成并维护至今,以下简称XCode. 整个系列教程会大量结合示例代码和 ...

  4. (二十二)golang--时间和日期相关函数

    时间的常量,可以获得指定时间单位 Unix和UnixNano   小例子:统计函数运行的时间:

  5. HBase的java操作,最新API。(查询指定行、列、插入数据等)

    关于HBase环境搭建和HBase的原理架构,请见笔者相关博客. 1.HBase对java有着较优秀的支持,本文将介绍如何使用java操作Hbase. 首先是pom依赖: <dependency ...

  6. CodeForce 577B Modulo Sum

    You are given a sequence of numbers a1, a2, ..., an, and a number m. Check if it is possible to choo ...

  7. css3的user-select属性设置文本内容能否被选择

    CSS3中提供了个user-select属性来设置或检索是否允许用户选中文本. 语法 user-select:none | text | all | element. 默认值:text. 适用性:除替 ...

  8. 在 EF Core 中 Book 实体在新增、修改、删除时,给 LastUpdated 字段赋值。

    直接贴代码: public class MenusContext : DbContext { public static class ColumnNames { public const string ...

  9. 2.将视图添加到 ASP.NET Core MVC 应用

    在本部分中,将修改 HelloWorldController 类,进而使用 Razor 视图文件来顺利封装为客户端生成 HTML 响应的过程. 当前,Index 方法返回带有在控制器类中硬编码的消息的 ...

  10. webapi 集成swagger

    参考资料:Stack Overflow 我自己写的demo:SwaggerDemoApi 在已有的webapi项目或者创建webapi项目中,打开nuget管理器 搜索:swagger 安装截图中的插 ...