【leetcode】Maximum Subarray (53)
1. Maximum Subarray (#53)
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.
简要解析:
本道题主要考查最大连续子序列和的求解方式。
我们可以这样考虑:当我们从头到尾遍历这个数组的时候,对于数组里的一个整数,它有两种选择:1. 加入之前的SubArray;2. 自己另起一个 SubArray。
考虑到有这两种情况,如果之前SubArray的总体和大于0,则认为其对后续结果有益,选择加入之前的SubArray
如果之前SubArray的总体和为0或者小于0,则认为其对后续结果无益,甚至是有害(小于0时),这种情况下选择以这个数字开始,另起一个 SubArray。
设状态为f[j],表示以 S[j] 结尾的最大连续子序列和,则状态转移方程如下:
f[j] = max{f[j −1] + S[j],S[j]}, 其中1 ≤ j ≤ n
target = max{f[j]}, 其中1 ≤ j ≤ n
n 情况一:S[j] 不独立,与前面的某些数组成一个连续子序列,则最大连续子序列和为 f[j −1] + S[j]。
n 情况二:S[j] 独立划分成为一段,即连续子序列仅包含一个数 S[j],则最大连续子序列和为 S[j]。
实现代码:
// LeetcodeTest.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <iostream>
#include <cstdio>
#include <climits>
#include <ctime>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <cstdlib>
#include <windows.h>
#include <string>
#include <cstring> using namespace std; class Solution {
public:
int maxSubArray(vector<int>& nums) {
int n = nums.size();
int result = nums[0], count=0;
for(int i=0; i<n; i++){
count +=nums[i];
result = max(result,count);
if(count<0)
count=0;
}
return result;
}
}; int main(){
vector<int>nums;
for(int i=0; i<6; i++){
nums.push_back(i);
}
Solution s;
cout<<s.maxSubArray(nums)<<endl;
system("pause");
return 0;
}
【leetcode】Maximum Subarray (53)的更多相关文章
- 【leetcode】Maximum Subarray
Maximum Subarray Find the contiguous subarray within an array (containing at least one number) which ...
- 【LeetCode】Maximum Subarray(最大子序和)
这道题是LeetCode里的第53道题. 题目描述: 给定一个整数数组 nums ,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和. 示例: 输入: [-2,1,-3,4,-1 ...
- 【Leetcode】【Medium】Maximum Subarray
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- 【LeetCode】Maximum Product Subarray 求连续子数组使其乘积最大
Add Date 2014-09-23 Maximum Product Subarray Find the contiguous subarray within an array (containin ...
- 【Leetcode】Maximum Product Subarray
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- 【数组】Maximum Subarray
题目: Find the contiguous subarray within an array (containing at least one number) which has the larg ...
- 【LeetCode】713. Subarray Product Less Than K 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/subarray ...
- 【leetcode】Maximum Gap
Maximum Gap Given an unsorted array, find the maximum difference between the successive elements in ...
- 【leetcode】Maximum Gap(hard)★
Given an unsorted array, find the maximum difference between the successive elements in its sorted f ...
随机推荐
- iOS真机调试引入第三方库(如友盟等)编译时候,出现错误提示
用Xcode 7 beta 3在真机(iOS 8.3)上运行一下工程,结果发现工程编译不过.看了下问题,报的是以下错误: MARK:解决方法:在building Setting 中设置bitCode ...
- php环境的搭建
Windows下php作为Apache的子模块加载 1.安装Apache后,找到httpd.conf文件,加入下列三行 #将php作为Apache的一个模块来处理LoadModule php5_mod ...
- 整块div设置为超链接进行界面跳转
鼠标点击当前整块DIV任意一个地方均可进行页面跳转,如果复制过去的代码不能用,请注意双引号和单引号,是否为英文状态下的输入法填写出来的. 1.跳转至新建页面 <div class="& ...
- Servlet实现asp.net中的Global.asax启动事件(Servlet和Listener使用)
1.Java Web中没有像asp.net的全局启动事件,但是可以通过web.xml中的load-on-startup节点来控制Servlet的开机启动和启动次数.web.xml详细配置参考:http ...
- SQL Server数据库常用函数
好久没学习新知识了.今天学了下sql的一些常用语句.人还是需要不断学习进步的 否则只能停滞不前. 先从最简单的一句开始说起吧. select *from 表名 这里*的含义 表示了表的各字段,以逗号隔 ...
- IE浏览器不能访问网页,google可以访问
现象:google浏览器可以进行网络访问,ie不能访问 原因:代理服务修改了局域网配置脚本 解决: Internet选项---连接---局域网设置: 去除勾选 “使用自动配置脚本”
- TypeScript Function(函数)
在JavaScript中,函数是构成任何应用程序的基础块.通过函数,你得以实现建立抽象层.模仿类.信息隐藏和模块化.在TypeScript中,虽然已经存在类和模块化,但是函数依旧在如何去"处 ...
- [Android]Volley源码分析(五)
前面几篇通过源码分析了Volley是怎样进行请求调度及请求是如何被实际执行的,这篇最后来看下请求结果是如何交付给请求者的(一般是Android的UI主线程). 类图:
- docker入门记录1
一. 什么是Docker 1.英文意思是集装箱,很形象.直白点就是将程序运行环境打包在一个箱子里,然后箱子扔到哪里,里边的程序都可以运行.这样以来一个显而易见的好处是:和以前的开发环境等相比,你不用每 ...
- 用U盘安装系统的好用的PE系统:通用PE V6.1下载
用U盘安装系统的好用的PE系统:通用PE V6.1下载 PE是一款用其他介质(我们最常用的是U盘)启动安装电脑系统的简易操作系统,在XP系统中 最经典的是扬州老毛桃出品的只有100多兆的XP内核的PE ...