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] ...
随机推荐
- 佛祖保佑永无bug
世界最难懂C语言代码竞赛: // _ooOoo_ // o8888888o // 88" . "88 // (| -_- |) // O\ = /O // ____/`---'\_ ...
- JDBC--Result 获取返回集合
package jdbc.chap05; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql. ...
- 正则表达式—RegEx(RegularExpressio)(一)
今日随笔,想和大家分享一下正则表达式的相关知识. 先不说概念性的东西,举一个例子再说. 验证你输入的邮政编码 ,你输入的邮政编码必须是六位的数字. while (true) { Console.Wri ...
- 【BZOJ2329/2209】[HNOI2011]括号修复/[Jsoi2011]括号序列 Splay
[BZOJ2329/2209][HNOI2011]括号修复/[Jsoi2011]括号序列 题解:我们的Splay每个节点维护如下东西:左边有多少多余的右括号,右边有多少多余的左括号,同时为了反转操作, ...
- go练习4--json 序列号反序列化
//定义结构体 //首字母大写 , json:"msg_id" 是 tag type Message struct { MsgId string `json:"msg_i ...
- java获取年份的第一天和最后一天
Calendar cal = Calendar.getInstance();cal.set(Calendar.MONTH, 0);cal.set(Calendar.DATE, 1);String da ...
- PHP中文字数限制:中文字符串截取(mb_substr)
一.中文截取:mb_substr() mb_substr( $str, $start, $length, $encoding ) $str,需要截断的字符串 $start,截断开始处,起始处为0 $l ...
- C#6.0语法特性
1.自动属性初始化的改进(有用) 原来的用法(声明时无法同时初始化),例如: class MyClass { public int Age { get; set; } public string Na ...
- 【node】------module.exports&&exports之间的区别------【巷子】
1.再讲module.exports 与exports之间的区别的时候我们先来回顾一下js里面的引用传递 001.引用传递 var arr = [10,20,30]; var newarr = arr ...
- 170511、Spring IOC和AOP 原理彻底搞懂
Spring提供了很多轻量级应用开发实践的工具集合,这些工具集以接口.抽象类.或工具类的形式存在于Spring中.通过使用这些工具集,可以实现应用程序与各种开源技术及框架间的友好整合.比如有关jdbc ...