【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 ...
随机推荐
- 《数据密集型应用系统设计》读书笔记-ch1可靠、可扩展与可维护的应用系统
我们以Twitter为例,使用其2012年11月发布的数据.Twitter的两个典型业务操作是: - 发布tweet消息: 用户可以快速推送新消息到所有的关注者,平均大约4.6k request/se ...
- unity动态加载FBX模型(Http下载到Rescources文件,场景Load直接调用):
using UnityEngine; using System.Collections; using System.IO; using System.Net; using System; using ...
- 封装element-ui的dialog组件
封装组件: <template> <div class="dialog-container"> <el-dialog title="titl ...
- JetBrains PyCharm(Professional版本)的下载、安装和初步使用
不多说,直接上干货! 首先谈及这款软件,博主我用的理由:搞机器学习和深度学习! 想学习Python的同学们,在这里隆重介绍一款 Python 的开发工具 pyCharm IDE.这是我最喜欢的 Pyt ...
- frames的对象兼容性获取以及跨域实现数据交换(js文件的加载判断)
1.document.frames()与document.frames[]的区别 <html> <body> <iframe id="ifr_1" n ...
- hibernate 中对象的3种状态总结
1.Hibernate把对象分文三种状态:Transient(临时状态).Persistent(持久化状态).Detached(游离状态). 1)Transient:刚刚new出来的对象,就是Tran ...
- preg_match()——php
第一,让我们看看两个特别的字符:‘^’和‘$’他们是分别用来匹配字符串的开始和结束,以下分别举例说明: "^The": 匹配以 "The"开头的字符串; &qu ...
- Codeforces 809D. Hitchhiking in the Baltic States
Description 给出 \(n\) 个数 \(a_i\),每一个数有一个取值 \([l_i,r_i]\) ,你来确定每一个数,使得 \(LIS\) 最大 题面 Solution 按照平时做法,设 ...
- 公司管理系列--80% of Your Culture is Your Founder(FaceBook)
80% of Your Culture is Your Founder When Molly Graham joined Facebook in 2008, the company still ...
- PHP学习7——文件系统
主要内容: 打开和关闭文件 文件类型 文件处理 目录处理 访问远程文件 文件锁定 文件上传 数据除了可以存储在数据库中,我们主要的还是存储在文件中,而且存储在文件中更加的方便直接. 打开和关闭文件 打 ...