最大字串和问题(Maximum Subarray)
问题描述:
ind 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 MaxSubarray
{
public int maxSubArray(int[] nums)
{
int sum = nums[0];
int max = nums[0];
for(int i = 1; i < nums.length; i ++)
{
if(sum < 0)
{
sum = 0;
}
sum += nums[i];
if(max < sum)
{
max = sum;
}
}
return max;
}
//穷举法O(n^3)
public int maxSubArray1(int[] nums)
{
int max = 0;
for(int i = 0; i < nums.length; i ++)
{
for(int j = i; j < nums.length; j ++)
{
int sum = 0;
for(int k = i; k < j; k ++)
{
sum += nums[k];
}
if(sum > max)
{
max = sum;
}
}
}
return max;
}
//O(n^2)
public int maxSubArray2(int[] nums)
{
int max = 0;
for(int i = 0; i < nums.length; i ++)
{
int sum = 0;
for(int j = i; j < nums.length; j ++)
{
sum += nums[j];
if(sum > max)
{
max = sum;
}
}
}
return max;
}
}
最大字串和问题(Maximum Subarray)的更多相关文章
- leetCode 53.Maximum Subarray (子数组的最大和) 解题思路方法
		Maximum Subarray Find the contiguous subarray within an array (containing at least one number) whic ... 
- [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/ 本来打算自己写的,后来看到上述链接的博客已经说得很清楚了,就不重 ... 
- 3月7日 Maximum Subarray
		间隔2天,继续开始写LeetCodeOj. 原题: Maximum Subarray 其实这题很早就看了,也知道怎么做,在<编程珠玑>中有提到,求最大连续子序列,其实只需要O(n)的复杂度 ... 
随机推荐
- webpack 构建项目入门
			参考http://www.cnblogs.com/eyunhua/p/6398885.html ---------------------------------------------------- ... 
- Oracle数据库模型(OLAP/OLTP)
			数据库模型 选择数据库模型: 联机事务处理OLTP(on-line transaction processing) OLTP是传统的关系数据库的主要应用,基本的.日常的事务处理.例如银行交易. OLT ... 
- Powered by Flink
			Apache Flink: Powered by Flink https://flink.apache.org/poweredby.html Powered by Flink Apache Flink ... 
- window.location.href = window.location.href  window.location.reload()
			w 0-会议预订提交了预订日期,预订成功后默认显示仅显示当前日期的新页面若显示预定日的信息,则可以对预定日存入cookie: http://stackoverflow.com/questions/24 ... 
- python基础之类的继承与派生、组合、接口与归一化设计、抽象类、子类中调用父类方法
			一.什么是继承 继承是一种创建新的类的方式,新建的类可以继承自一个或者多个父类,原始类称为基类或超类,新建的类称为派生类或子类. 派生:子类继承了父类的属性,然后衍生出自己新的属性,如果子类衍生出的新 ... 
- Learning How to Learn学习笔记(转)
			add by zhj: 工作中提高自己水平的最重要的一点是——快速的学习能力.这篇文章就是探讨这个问题的,掌握了快速学习能力的规律,你自然就有了快速学习能力了. 原文:Learning How to ... 
- 我的Android进阶之旅------>解决:debug-stripped.ap_' specified for property   'resourceFile' does not exist.
			1.错误描述 更新Android Studio到2.0版本后,出现了编译失败的问题,我clean project然后重新编译还是出现抑郁的问题,问题具体描述如下所示: Error:A problem ... 
- Android--Apache HttpClient(2种实现)
			前言 上一篇文章介绍了使用HttpURLConnection来完成对于HTTP协议的支持,现在介绍一个新的方式来访问Web站点,那就是HttpClient. HttpClient是Apache开源组织 ... 
- Linux学习笔记(13)linux软件安装rpm与yum--理论篇
			该文章linux知识点如下 1.linux中 软件包介绍 2.linux源码软件安装 3.linux二进制软件安装 4.linux rpm软件包管理 5.linux yum软件包管理 1.linux中 ... 
- PHP获取客户端的IP
			function getClientIP(){ global $ip; if (getenv("HTTP_CLIENT_IP")) $ip = geten ... 
