编程习题——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.
public class Test08 {
public static void main(String[] args) {
int[] nums = { -2, 1, -3, 4, -1, 2, 1, -5, 4 };
int[] sums = new int[nums.length];
int max = nums[0];
sums[0] = nums[0];
for (int i = 1; i < nums.length; i++) {
sums[i] = Math.max(nums[i], nums[i] + sums[i - 1]);
max = Math.max(max, sums[i]);
}
System.out.println(max);
}
}
编程习题——Maximum Subarray的更多相关文章
- 3月7日 Maximum Subarray
间隔2天,继续开始写LeetCodeOj. 原题: Maximum Subarray 其实这题很早就看了,也知道怎么做,在<编程珠玑>中有提到,求最大连续子序列,其实只需要O(n)的复杂度 ...
- [LintCode] Maximum Subarray 最大子数组
Given an array of integers, find a contiguous subarray which has the largest sum. Notice The subarra ...
- 【leetcode】Maximum Subarray (53)
1. Maximum Subarray (#53) Find the contiguous subarray within an array (containing at least one nu ...
- 算法:寻找maximum subarray
<算法导论>一书中演示分治算法的第二个例子,第一个例子是递归排序,较为简单.寻找maximum subarray稍微复杂点. 题目是这样的:给定序列x = [1, -4, 4, 4, 5, ...
- LEETCODE —— Maximum Subarray [一维DP]
Maximum Subarray Find the contiguous subarray within an array (containing at least one number) which ...
- 【leetcode】Maximum Subarray
Maximum Subarray Find the contiguous subarray within an array (containing at least one number) which ...
- maximum subarray problem
In computer science, the maximum subarray problem is the task of finding the contiguous subarray wit ...
- (转)Maximum subarray problem--Kadane’s Algorithm
转自:http://kartikkukreja.wordpress.com/2013/06/17/kadanes-algorithm/ 本来打算自己写的,后来看到上述链接的博客已经说得很清楚了,就不重 ...
- LeetCode: Maximum Product Subarray && Maximum Subarray &子序列相关
Maximum Product Subarray Title: Find the contiguous subarray within an array (containing at least on ...
随机推荐
- 通过 IP 访问谷歌
最近貌似谷歌都不能访问,对于我等经常使用谷歌的人说不是件好事,毕竟谷歌比百度专业,好在有解决办法: 1. 找到文件:C:\Windows\System32\drivers\etc\hosts 2. 把 ...
- 在centos7下安装mysql5.7
wget http://dev.mysql.com/get/mysql57-community-release-el7-9.noarch.rpmyum localinstall -y mysql57- ...
- English words
英语指路常用单词 the one-way street单行道traffic light红绿灯 fork road三叉路口intersection/crossroad 十字路口T road 丁字路口in ...
- Activity篇章参考
附上学习这部分知识的时候收集的一些比较好的链接: Task and backStack|Android Developer adb shell dumpsys activity 单个apk多进程 Ac ...
- 项目中引用ThinkPHP框架
ThinkPHP是一个宽度.兼容且简单的国产的轻量级框架,具有优良的性能,并且非常注重易用性. 那么,我们该如何将ThinkPHP引入自己的项目中,使得自己的项目可以使用这款优良的框架呢? 首先介绍下 ...
- chrome调试ajax
network可以查看ajax的信息.非常的实用.
- GDOI2015酱油记
GDOI2015酱油记 今年的GDOI在北江举行,比赛前一天坐了5小时的车才到,幸好忍住了,没有在车上吐. Day 1 刚到电教楼,看完考室后,第一时间找厕所,结果发现只有一楼有厕所,坑爹我的考室在三 ...
- python-认识Socket[入门篇]
什么是socket 网络上的两个程序通过一个双向的通信连接实现数据的交换,这个连接的一端称为一个socket.socket通常也称作"套接字",用于描述IP地址和端口,是一个通信链 ...
- lucene定义自己的分词器将其分成单个字符
问题描写叙述:将一句话拆分成单个字符.而且去掉空格. package com.mylucene; import java.io.IOException; import java.io.Reader; ...
- 权威指南之脚本化jquery
jqury函数 jquery()($())有4种不同的调用方式 第一种是最常用的调用方式是传递css选择器(字符串)给$()方法.当通过这种方式调用时,$()方法会返回当前文档中匹配该选择器的元素集. ...