Maximum Subarray
2015-07-25 18:57
316 查看
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.
public class Solution {
public int maxSubArray(int[] A) {
int newsum=A[0];
int max=A[0];
for(int i=1;i<A.length;i++){
newsum=Math.max(newsum+A[i],A[i]);
max= Math.max(max, newsum);
}
return max;
}
}
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.
public class Solution {
public int maxSubArray(int[] A) {
int newsum=A[0];
int max=A[0];
for(int i=1;i<A.length;i++){
newsum=Math.max(newsum+A[i],A[i]);
max= Math.max(max, newsum);
}
return max;
}
}
相关文章推荐
- Linux lvm 管理
- UVa 10954 - Add All(最优二叉树)
- iOS开发-Block回调
- 新接口
- hdu 5294 Tricks Device
- UVA - 11059 Maximum Product
- 作业
- 股票作手回忆录Digest(转)
- Arduino 数码管LED驱动器 阵列方法
- 常用的SQL语句
- poj 3268 Silver Cow Party(最短路)
- java中的匿名内部类总结
- HNOJ13303字符串%3题
- HorizontalScrollView 初始化滚动的实现
- struts2之如何向服务器上传文件
- uva live 6827 Galaxy collision
- java 内存空间
- 全排列实现
- 二分搜索 POJ 2456 Aggressive cows
- 梯度下降法