Leetcode-maximum-subarray
2016-06-30 13:27
381 查看
题目描述
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.
click to show more practice.
More practice:
If you have figured out the O(n) solution, try coding another solution using the divide and conquer approach, which is more subtle.
动态规划入门级题目:最大子数组
前面的博文中其实介绍了这一题,我在调试的时候,仍然出现了问题,子数组连续的,这点很重要。
public class Solution {
public int maxSubArray(int[] A) {
int max = A[0];
int res = A[0];
for(int i=1; i<A.length; i++){
res = Math.max(A[i], res+A[i]); //这里需要注意,是A[i]和res+A[i]的比较,不是和A[i]+max的比较,这点非常重要。
max = Math.max(res, max);
}
return max;
}
}
相关文章推荐
- [置顶] Acm课程总结
- 【六月英语——空空的】
- __FILE__,__LINE__
- 【Azure Active Directory】单一登录 (SAML 协议)
- 如何在Android 上面实现GATT Server
- C/C++中的struct
- iOS_childController篇
- Https Tomcat CSR生成 以及相关操作
- Python 黑帽子 snffer ip header decoder
- 保留三位小数之妙
- Android 框架练成 教你打造高效的图片加载框架
- php
- Lua中的模块(module)和包(package)详解
- [leetcode] 371. Sum of Two Integers 解题报告
- springmvc复杂表单的编写处理
- LeetCode - 7. Reverse Integer
- 软件开发人员和程序员语录
- GridView高度填充屏幕
- JDK动态代理
- 【leetcode】9. Palindrome Number