【Leetcode】53. Maximum Subarray
题目地址:
https://leetcode.com/problems/maximum-subarray/description/
题目描述:
经典的求最大连续子数组之和。
解法:
遍历这个vector,每次循环累加当前数值,同时更新最大值,若当前的累加和是负数,需要更新为0,因为在进行下一次累加求和的时候,
无论下一个数是正还是负,加上上一次的为负的和,都将变得更小,因而下一次求和时直接从当前值开始计算。
代码:
class Solution {
public:
int maxSubArray(vector<int>& nums) {
if (nums.size() == ) {
return nums.at();
}
vector<int>::iterator iter = nums.begin();
int max = -( << );
int sum = ;
for ( ; iter != nums.end(); iter++)
{
sum += *iter;
if (sum > max) {
max = sum;
}
if (sum < ) {
sum = ;
}
}
return max;
}
};
【Leetcode】53. Maximum Subarray的更多相关文章
- 【LeetCode】53. Maximum Subarray (2 solutions)
Maximum Subarray Find the contiguous subarray within an array (containing at least one number) which ...
- 【LeetCode】53. Maximum Subarray 最大子序和 解题报告(Python & C++ & Java)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力解法 动态规划 日期 题目地址: https:/ ...
- 【LeetCode】053. Maximum Subarray
题目: Find the contiguous subarray within an array (containing at least one number) which has the larg ...
- 【一天一道LeetCode】#53. Maximum Subarray
一天一道LeetCode系列 (一)题目 Find the contiguous subarray within an array (containing at least one number) w ...
- 【leetcode】1186. Maximum Subarray Sum with One Deletion
题目如下: Given an array of integers, return the maximum sum for a non-empty subarray (contiguous elemen ...
- 【LeetCode】718. Maximum Length of Repeated Subarray 解题报告(Python)
[LeetCode]718. Maximum Length of Repeated Subarray 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxu ...
- 【leetcode】998. Maximum Binary Tree II
题目如下: We are given the root node of a maximum tree: a tree where every node has a value greater than ...
- 【LeetCode】895. Maximum Frequency Stack 解题报告(Python)
[LeetCode]895. Maximum Frequency Stack 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxueming ...
- 【LeetCode】662. Maximum Width of Binary Tree 解题报告(Python)
[LeetCode]662. Maximum Width of Binary Tree 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.co ...
随机推荐
- SET NOCOUNT 的用法
SET NOCOUNT 使返回的结果中不包含有关受 Transact-SQL 语句影响的行数的信息. 语法 SET NOCOUNT { ON | OFF } 注释 当 SET NOCOUNT ...
- C#中使用typeof关键字和GetType()获取类的内部结构(反射机制)
一.问题描述 java有反射机制,C#也有反射机制,在C#中typeof关键字用于获取类型的System.Type对象,该对象的GetMethods()方法可以得到类型中定义的方法对象的计集合,调用方 ...
- Android 9.0 Http不能访问网络
最近在做公司产品,一期完成,打包给测试,然后....一台手机连服务器都访问不了看日志如下: UnityWebRequest返回code:0,显示Unknow error 服务器接口是http://非域 ...
- linux都有哪些运行级别?
答: 一共有七种运行级别,如下: 0 – System halt i.e the system can be safely powered off with no activity. 1 – Sing ...
- flutter 右滑返回上一页
import 'package:flutter/material.dart'; import 'package:flutter_app/pages/SplashScreen.dart'; import ...
- opencv3.4.6 cmake
Selecting Windows SDK version to target Windows 10.0.16299. Found PythonInterp: N:/Anaconda3/install ...
- linux传输文件lrzsz
linux传输文件
- WebGL高级编程:开发Web3D图形 PDF(中文版带书签)
WebGL高级编程:开发Web3D图形 目录 WebGL简介11.1 WebGL基础11.2 浏览器3D图形吸引人的原因21.3 设计一个图形API31.3.1 即时模式API31.3.2 保留模式A ...
- java 特殊字符处理
// 去除富文本中的html标签 // <p>段落替换为换行 content = content.replaceAll("<p .*?>", "\ ...
- Delphi 中使用计算出的字段
在很多情况下,我们需要的数据与数据库中其它字段的数据相关,例如订单的金额为数量与单价的乘积.在应用程序中,若要在显示订单具体条目的同时显示金额,通常要创建一个字段,在显示该字段之前先进行乘法运算,将金 ...