【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 ...
随机推荐
- sizeof(数组名) 与 数组长度
int a[] = {1, 2, 3, 4}; cout << sizeof(a); //16 char b[] = "abc"; cout << size ...
- inline元素、inline-block元素在float、position:fixed、position:absolute之后出现的问题
我们知道内联元素是不能设置宽.高的,但是一旦使其脱离了文档流,就可以了,这是因为它已经变成了块级元素. 例1: <!DOCTYPE html> <html lang="en ...
- bug缺陷级别定义
缺陷定义: 出现以下缺陷测试定义为致命 bug l 系统无响应处于死机状态. l 点击某个菜单后出现“The page cannot be displayed”或者返回异常错误. l 进行某 ...
- jquery jgrid filterToolBar beforeSearch 修改postData
beforeSearch: function() { var posted_data = $("#mygrid").jqGrid('getGridParam,'postData') ...
- c#,利用WPF的ScaleTransform和TranslateTransform实现图片的缩放效果
一.缩放要求 1.缩放对象在可视区域内,已对象的中心点进行缩放. 2.缩放对象比可视区大,并且对象各边界都在可视区外围,那么已鼠标位置进行缩放. 3.缩放对象比可视区大,但是缩放后某一边界在可视区的对 ...
- 问题集录06--SpringBoot创建Maven项目
1. 如下图,打开idea之后,file -> new -> project2. 如下图,在弹出的new project 页面,选择maven -> 勾选Create from ar ...
- [转]what’s the difference between @Component ,@Repository & @Service annotations in Spring
原文地址:https://www.cnblogs.com/softidea/p/6070314.html @Component is equivalent to <bean> @Servi ...
- spring-boot配置热更新
spring boot使用的时候会遇到不断重启启动类的情况,热更新可以免去手动重启的烦恼,spring boot应用配置热更新主要有两种方式: 1.使用springloaded 配置pom.xml文件 ...
- Cheatsheet: 2017 04.01 ~ 04.30
Other ReactXP - A LIBRARY FOR BUILDING CROSS-PLATFORM APPS Merging vs. Rebasing Better Git configura ...
- Browser对象之Window对象
对象属性 对象方法 setInterval() 按照指定的周期(以毫秒计)来调用函数或计算表达式. setInterval() 方法可按照指定的周期(以毫秒计)来调用函数或计算表达式. setInte ...