问题描述:

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)的更多相关文章

  1. leetCode 53.Maximum Subarray (子数组的最大和) 解题思路方法

    Maximum Subarray  Find the contiguous subarray within an array (containing at least one number) whic ...

  2. [LintCode] Maximum Subarray 最大子数组

    Given an array of integers, find a contiguous subarray which has the largest sum. Notice The subarra ...

  3. 【leetcode】Maximum Subarray (53)

    1.   Maximum Subarray (#53) Find the contiguous subarray within an array (containing at least one nu ...

  4. 算法:寻找maximum subarray

    <算法导论>一书中演示分治算法的第二个例子,第一个例子是递归排序,较为简单.寻找maximum subarray稍微复杂点. 题目是这样的:给定序列x = [1, -4, 4, 4, 5, ...

  5. LEETCODE —— Maximum Subarray [一维DP]

    Maximum Subarray Find the contiguous subarray within an array (containing at least one number) which ...

  6. 【leetcode】Maximum Subarray

    Maximum Subarray Find the contiguous subarray within an array (containing at least one number) which ...

  7. maximum subarray problem

    In computer science, the maximum subarray problem is the task of finding the contiguous subarray wit ...

  8. (转)Maximum subarray problem--Kadane’s Algorithm

    转自:http://kartikkukreja.wordpress.com/2013/06/17/kadanes-algorithm/ 本来打算自己写的,后来看到上述链接的博客已经说得很清楚了,就不重 ...

  9. 3月7日 Maximum Subarray

    间隔2天,继续开始写LeetCodeOj. 原题: Maximum Subarray 其实这题很早就看了,也知道怎么做,在<编程珠玑>中有提到,求最大连续子序列,其实只需要O(n)的复杂度 ...

随机推荐

  1. bootstrap 媒体查询

    //各类设备的分辨率 /*超小设备(手机,小于768px)*/ /* Bootstrap 中默认情况下没有媒体查询 */ /*超小型设备(小于768px)*/ @media (min-width:@s ...

  2. Akka Essentials - 2

    Actors Defining an actor class MyActor extends Actor { def receive = { } } In Scala, the receive blo ...

  3. Storm-源码分析汇总

    Storm Features Storm 简介 Storm Topology的并发度 Storm - Guaranteeing message processing Storm - Transacti ...

  4. Visualizing wave interference using FireMonkey(很美)

      Visualizing wave interference using FireMonkey By: Anders Ohlsson Abstract: This article discusses ...

  5. BZOJ2648: SJY摆棋子&&2716: [Violet 3]天使玩偶

    BZOJ2648: SJY摆棋子 BZOJ2716: [Violet 3]天使玩偶 BZOJ氪金无极限... 其实这两道是同一题. 附上2648的题面: Description 这天,SJY显得无聊. ...

  6. Windows 7 下 Node.js 连接 Oracle

    原创作者: sailtseng 1. 安装 Oracle 11g express  详见: <Windows 7 x64 安装 Oracle 11g Express> 2. 安装 Micr ...

  7. cas添加验证码

    cas添加验证码,折腾了好久,终于整理好了,很大部分都是借鉴http://binghejinjun.iteye.com/blog/1255293这个的.但是他的有一个很不好的地方就是不能提升验证码错误 ...

  8. Centos学习笔记1-基本部分

    1:查看系统的使用状态: 谁在线:who 网络连接状态:netstat  –a 后台执行程序:ps –aux 2:关机 关机:shutdown   或者 shutdown –h now 重启:rebo ...

  9. java反射基础知识(三)

    原文地址:http://tutorials.jenkov.com/java-reflection/index.html http://www.cnblogs.com/penghongwei/p/329 ...

  10. github使用ssh秘钥的好处以及设置(转)

    git使用https协议,每次pull,push都要输入密码,使用git协议,使用ssh秘钥,可以省去每次输密码 大概需要三个步骤:一.本地生成密钥对:二.设置github上的公钥:三.修改git的r ...