Maximum Subarray
2016-06-19 11:39
232 查看
经典dp
public class Solution {
public int maxSubArray(int[] nums) {
int[] sum = new int[nums.length];
int max = nums[0];
sum[0] = nums[0];
for (int i = 1; i < nums.length; i++) {
sum[i] = Math.max(nums[i], sum[i - 1] + nums[i]);
max = Math.max(sum[i], max);
}
return max;
}
}
Find the contiguous subarray within an array (containing at least one number) which has the largest sum.
For example, given the array
the contiguous subarray
public class Solution {
public int maxSubArray(int[] nums) {
int[] sum = new int[nums.length];
int max = nums[0];
sum[0] = nums[0];
for (int i = 1; i < nums.length; i++) {
sum[i] = Math.max(nums[i], sum[i - 1] + nums[i]);
max = Math.max(sum[i], max);
}
return max;
}
}
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.
相关文章推荐
- centos虚拟机安装后网络配置
- 阿里云服务器web应用安全-XSS攻击
- Linux 设备驱动的并发控制
- 学习进度条——第十六周
- Android应用Context详解及源码解析
- MongoDB配制文件详解
- Java开发实用工具及组件应用
- 使用<br>标签分行显示文本
- C语言 百炼成钢23
- Python Moniotr Redis
- 主键约束设置Enable Novalidate时报错
- openstack的公共库(oslo)的使用
- 第十六周进度条
- 阻塞队列
- pyhon中的__all__
- 多个PDF文件合并为一个PDF文件
- 依赖、关联、聚合、组合等之间的关系
- 关于go语言的学习和使用
- Linux 设备驱动中的 I/O模型(一)—— 阻塞和非阻塞I/O
- 树莓派+aria2+yaaw搭建下载机