【Leetcode】【Medium】Maximum Subarray
Find the contiguous subarray within an array (containing at least one number) which has the largest sum.
For example, given the array [−2,1,−3,4,−1,2,1,−5,4]
,
the contiguous subarray [4,−1,2,1]
has the largest sum = 6
.
解题思路:
题目比较简单,leetcode中有多个类似题目;
从左向右遍历,计算当前遍历到的元素之和,如果和为负,说明这些被累加的元素不会对后面序列提供“正值”的贡献,因此和值清零,继续计算;
在计算和的过程中,不断记录能取到的最大值。
注意数组中全为负数的情况。
class Solution {
public:
int maxSubArray(vector<int>& nums) {
int max = nums[];
int cur_sum = ;
for (int i = ; i < nums.size(); ++i) {
cur_sum += nums[i];
if (cur_sum > max)
max = cur_sum;
if (cur_sum < )
cur_sum = ;
} return max;
}
};
【Leetcode】【Medium】Maximum Subarray的更多相关文章
- 【LeetCode题意分析&解答】40. Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- 【LeetCode题意分析&解答】37. Sudoku Solver
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...
- 【LeetCode题意分析&解答】35. Search Insert Position
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
- 【LeetCode】最大子阵列 Maximum Subarray(贪婪&分治)
描述: Given an integer array nums, find the contiguous subarray (containing at least one number) which ...
- ACM金牌选手整理的【LeetCode刷题顺序】
算法和数据结构知识点图 首先,了解算法和数据结构有哪些知识点,在后面的学习中有 大局观,对学习和刷题十分有帮助. 下面是我花了一天时间花的算法和数据结构的知识结构,大家可以看看. 后面是为大家 精心挑 ...
- 【LeetCode每天一题】Maximum Subarray(最大子数组)
Given an integer array nums, find the contiguous subarray (containing at least one number) which has ...
- 【leetcode刷题笔记】Maximum Depth of Binary Tree
Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...
- 【LeetCode算法题库】Day7:Remove Nth Node From End of List & Valid Parentheses & Merge Two Lists
[Q19] Given a linked list, remove the n-th node from the end of list and return its head. Example: G ...
- 【LeetCode算法题库】Day4:Regular Expression Matching & Container With Most Water & Integer to Roman
[Q10] Given an input string (s) and a pattern (p), implement regular expression matching with suppor ...
- 【LeetCode算法题库】Day3:Reverse Integer & String to Integer (atoi) & Palindrome Number
[Q7] 把数倒过来 Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Outpu ...
随机推荐
- 【爬虫】-爬取猫眼TOP100
原文崔庆才<python3网络爬虫实战> 本文为自学记录,如有侵权,请联系删除 目标: 熟悉正则表达式,以及爬虫流程 获取猫眼TOP100榜单 1.网站分析 目标站点为http://www ...
- maven打包时跳过测试类
记录 修改pom.xml文件 <project> [...] <build> <plugins> <plugin> <groupId>org ...
- Unity QualitySettings.vSyncCount 垂直同步数
QualitySettings.vSyncCount 垂直同步数 Description 描述 The VSync Count. 垂直同步数. The number of VSyncs that sh ...
- db2 tsm backup fails with rc–50(1)
2015-01-05-19.21.54.477532+000 E8484227A347 LEVEL: Error PID : 10027058 TID : ...
- (转)MySQL5.6主从复制技术
原文:http://www.cnblogs.com/ilifeilong/category/1014799.html MySQL5.6复制技术(4)-MySQL主从复制过滤参数 摘要: 复制的过滤主要 ...
- UI设计黄金法则
1. 网页用户界面(WUI - web user interface) 最佳设计经验与准则 以主页为基准,建立标准的层级结构 保持布局的一致性 将大版本的内容或过于复杂的任务分解为多个页面显示 将导航 ...
- 关于window.console&&console.log(123)的思考
一.JS的且运算记得最开始看到window.console&&console.log(123),当时知道能起什么作用但是没有深入研究,最近在研究后总算弄明白了.要理解这个,首先得明白三 ...
- lua三目运算符
lua的类似三目运算符用法 一般化的Lua三目运算为:(a and {b} or {c})[1] local v = (a and {b} or {c})[1]如果a为true,则 v = b 如果a ...
- C#控件随窗体大小改变而改变
几种方法:1.点击控件,属性,里面有一个Dock,选择Fill,就会变得和它的父容器一样大.而且会随之变化.2.点击控件,属性,里面有一个Anchor,选择Top,Right,Bottom,Left. ...
- TXSQL:云计算时代数据库核弹头——云+未来峰会开发者专场回顾
欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 5月23-24日,以"焕启"为主题的腾讯"云+未来"峰会在广州召开,广东省各级政府机构领导.海内外业 ...