53. 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
.
递归方程;
dp[i] = dp[i-1]+nums[i] ,dp[i-1]>0
dp[i] = nums[i] ,else
class Solution {
public int maxSubArray(int[] nums) {
int dp[] = new int[nums.length+1];
int max = nums[0];
dp[0] = nums[0];
for(int i =1;i<nums.length;i++){
if(dp[i-1]<=0)
dp[i] = nums[i];
else
dp[i] = dp[i-1]+nums[i];
max = Math.max(dp[i],max);
}
return max;
}
}
53. Maximum Subarray(动态规划 求最大子数组)的更多相关文章
- 小旭讲解 LeetCode 53. Maximum Subarray 动态规划 分治策略
原题 Given an integer array nums, find the contiguous subarray (containing at least one number) which ...
- LeetCode 53. Maximum Subarray(最大的子数组)
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- 53. Maximum Subarray(动态规划)
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- [LeetCode] Maximum Product Subarray 求最大子数组乘积
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- [LeetCode] 152. Maximum Product Subarray 求最大子数组乘积
Given an integer array nums, find the contiguous subarray within an array (containing at least one n ...
- 刷题53. Maximum Subarray
一.题目说明 题目是53. Maximum Subarray,求最长连续子序列最大和.难度是Easy! 二.我的解答 Easy的题目,居然没做出来. 后来看了用dp方法,其中dp[i]表示以第i个元素 ...
- [LeetCode] 53. Maximum Subarray 最大子数组
Given an integer array nums, find the contiguous subarray (containing at least one number) which has ...
- [leetcode]53. Maximum Subarray最大子数组和
Given an integer array nums, find the contiguous subarray (containing at least one number) which has ...
- Leetcode#53.Maximum Subarray(最大子序和)
题目描述 给定一个序列(至少含有 1 个数),从该序列中寻找一个连续的子序列,使得子序列的和最大. 例如,给定序列 [-2,1,-3,4,-1,2,1,-5,4], 连续子序列 [4,-1,2,1] ...
随机推荐
- 转:Android文件操作总结
http://www.cnblogs.com/devinzhang/archive/2012/01/19/2327597.html http://blog.sina.com.cn/s/blog_5a4 ...
- 09python之运算
运算 算数运算: 两个数相除,得到商和余数的数组的内置方法 数据.__divmod__() 场景 数据库共99条数据,每个页面10条数据,需要多少个页面 >>> a = 98 ...
- 微信小程序 ui框架(辅助)
WeUi: https://weui.io/ https://github.com/weui/weui-wxss/ Wa-Ui: https://github.com/liujians/Wa-UI/w ...
- oracle12c创建用户和表空间出现的问题
Oracle12c 中,增加了可插接数据库的概念,即PDB,允许一个数据库容器(CDB)承载多个可插拔数据库(PDB).CDB全称为 ContainerDatabase,中文翻译为数据库容器,PDB全 ...
- ATL字符宏使用以及代码测试
// ATL_Convert.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <iostream> #incl ...
- springAOP实现(含实例)
需要用到的jar包: 1.XML方式实现: package cn.lxc.post; public class Intermediary { public void post(){ System.ou ...
- [分享]方便的 windbg 命令 - !list
Windows 内部的各种结构通常都会由双向链表串起来,用 !list 命令查看这些结构非常方便. 比如查看系统中的所有进程: lkd> !list -t nt!_LIST_ENTRY.Flin ...
- js部署中如何提高页面加载速度
1.合并js文件,减少http请求数量. 2.对js文件进行压缩 3.以gzip的形式提供js 4.使js文件可缓存 5.使用CDN
- Tomcat7.0无法启动解决方法[failed to start]
很奇怪的一个问题,Tomcat一直好好的,运行Servlet之后就报这个错: 为什么呢?在网上查都查不到解决方法,后来仔细检查了下Servlet,发现web.xml有个低级错误: 配置的Servlet ...
- CVE-2017-1000117命令注入验证
首先,我们来看第一个问题,就是ssh的一种操作. ssh -oProxyCommand=gnome-calculator xxx 问题的本质在于ssh会把//后的host那么部分带-号的按照参数指令去 ...