最大字串和问题(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)的复杂度 ...
随机推荐
- 支付宝app支付提示 系统繁忙,请稍后重试
v2版本的支付宝支付,无论怎么调试返回的都是系统繁忙,请稍后重试提示.经过对给的示例代码的仔细研究,最后发现是由于老版本的sign_type 不加入签名计算,而新版本的支付签名它是加入签名计算的.供大 ...
- 在java中public void与public static void有什么区别 ?
public void 修饰是非静态方法,该类方法属于对象,在对象初始化(new Object())后才能被调用:public static void 修饰是静态方法,属于类,使用类名.方法名直接调用 ...
- Extjs combobox 实现搜索框的效果
目的:使用combobox实现一个类似搜索框的效果,即用户输入内容后,出现相关下列列表,提供选择. 实现:extjs3 中combobox自身带这个功能即在remote模式下,store在load的时 ...
- Logstash Reference Getting started with Logstash
进阶功能_Logstash_数据采集_用户指南_日志服务-阿里云 https://help.aliyun.com/document_detail/49025.html Logstash Referen ...
- table width 决定 td width
w td width 有无在chrome edge ff 均未影响td实际宽度,td接近等比分配table width. <!doctype html> <html lang=&qu ...
- unix_timestamp 和 from_unixtime 时间戳函数 区别
1.unix_timestamp 将时间转化为时间戳.(date 类型数据转换成 timestamp 形式整数) 没传时间参数则取当前时间的时间戳 mysql> select unix_time ...
- IO 流之字节流和转换流
基本读取操作: InputStream(); OutputStream(); // 直接写入目的地中, 不需要 flush() 刷新 write(byte[] b); // 参数为 byte 数组 字 ...
- django模板复用 extends,block,include
template复用 extends block include render 参考:https://code.ziqiangxuetang.com/django/django-template.ht ...
- Leetcode 之 Exclusive Time of Functions
636. Exclusive Time of Functions 1.Problem Given the running logs of n functions that are executed i ...
- node.js---sails项目开发(6)--- 实现分页功能
只需要添加一个文件即可 api/blueprints/find.js 代码如下 /** * Module dependencies */ var util = require('util') ...