【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 ...
随机推荐
- iframe的src指向的内容不刷新
想任何一种办法让iframe的src的值有变化就可以了 $("#h5Content").attr("src","${h5.url}"+&qu ...
- python 椭球面
作者:chaowei wu链接:https://www.zhihu.com/question/266366089/answer/307037017来源:知乎著作权归作者所有.商业转载请联系作者获得授权 ...
- file_put_contents 和php://input 实现存储数据进图片中
<?php /** *Recieve p_w_picpath data **/ error_reporting(E_ALL); function get_contents() { $xmlstr ...
- Install LEDE on a BT Home Hub 5 / Plusnet One Router
Overview / Purpose of this guide These instructions are for aimed at users of Windows but a lot of t ...
- Cesium - Fabric 材质【转官网】
https://github.com/AnalyticalGraphicsInc/cesium/wiki/Fabric Fabric Hannah edited this page on 24 Dec ...
- Java IO系统--RandomAccessFile
RandomAccessFile 实现了DataOutput接口和DataInput接口.父类是Object,不继承任何的InputStream和OutStram. public class Rand ...
- PHP 输出两个指定日期之间的所有日期
function printDates($start,$end){ $dt_start = strtotime($start); $dt_end = strtotime($end); while ($ ...
- linux传输文件lrzsz
linux传输文件
- centos 安装最新版git
对个人而言,gitlab有点浪费资源,占内存太大,一个博客服务器,配置比较低,用gitlab太浪费了.(公司使用gitlab,这个适合公司团队使用) 前提条件,放行git端口,防火墙添加放行规则,将3 ...
- Python - 在CentOS7.5系统中安装Python3
注意:以下内容均使用root用户执行操作. 1-确认信息 # uname -a Linux localhost.localdomain 3.10.0-957.el7.x86_64 #1 SMP Thu ...