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] ...
随机推荐
- Python 入门(六)Dict和Set类型
什么是dict 我们已经知道,list 和 tuple 可以用来表示顺序集合,例如,班里同学的名字: ['Adam', 'Lisa', 'Bart'] 或者考试的成绩列表: [95, 85, 59] ...
- /var/log/cron
/var/log/cron 用来记录任务计划的运行日志,如下,分别表示: 任务运行的日期与时间 .在哪台主机上运行 .运行任务的程序[进程号] .任务运行的具体信息 [root@localhost ~ ...
- 将display设置为inline-block之后产生间隙然后换行问题的解决方法
在我们会用display的时候,inline-block肯定不陌生吧,我今天在做项目的时候,用了inline-block,使a标签可以自定义宽度,但是随之而来的问题就是换行的BUG,如下图 特地加了一 ...
- uiimageview 异步加载图片
dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void){ NSURL *url = ...
- java基础---->Java关于复制的使用(一)
这里简单记录一下java中关于浅复制和深复制的知识.很多时候,一个人选择了行走,不是因为欲望,也并非诱惑,他仅仅是听到了自己内心的声音. java中的复制clone方法 一.java对象的浅复制 一个 ...
- The 70th problem,UVa10396 Vampire Numbers
今天看Thinking in Java看到一个吸血鬼数的问题,于是查找UVa里也有类似的问题就动手写了先是用Java写的,不过WA了两次,然后没有发现错误,又用c++写的还是不行.最后发现要排序去重. ...
- apt-get install的默认安装路径
apt-get 命令 sudo apt-get update sudo apt-get install tree sudo apt-get -y install tree //跳过系统提示,直接安装. ...
- 高中生的IT之路-1.1自序
近几年来越来越多的人问我关于 高中生要不要读大学.大学选择专业.毕业后的择业问题,索性我不如把我对这几方面的理解写出来,如果有幸能帮助到更多的人,那也算是个人对社会做出了一点贡献. ...
- WCF(四) 深入契约
服务契约中的请求-响应操作 1.请求-响应模式 [OperationContract]//1默认就是 请求-相应 Requst- Replay DateTime GetDateTime(); [Ope ...
- angular -- 自定义指令和模板
angular 可以自定义一些指令,来简化我们前端的工作量. 第一种:简单指令示例: <h3>自定义指令</h3> <sheng></sheng> &l ...