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 static int maxSubArray(int[] A)
{
int max=A[0];
for(int i=0;i<A.length;i++)
{
int sum=0;
for(int j=i;j<A.length;j++)
{
sum+=A[j];
if(sum>max) max=sum; }
} return max;
}

O(n):

 public static int maxSubArray(int[] A)
{
int max=A[0];
int sum=0;
for(int i=0;i<A.length;i++)
{
sum+=A[i];
if(sum>max)
max=sum; if(sum<0)
sum=0;
}
return max;
}

Maximum Subarray (JAVA)的更多相关文章

  1. 53. Maximum Subarray (JAVA)

    iven an integer array nums, find the contiguous subarray (containing at least one number) which has ...

  2. LeetCode 53. Maximum Subarray(最大的子数组)

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

  3. LeetCode: Maximum Subarray 解题报告

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

  4. LeetCode 53. 最大子序和(Maximum Subarray)

    53. 最大子序和 53. Maximum Subarray 题目描述 给定一个整数数组 nums,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和. LeetCode53. M ...

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

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

  6. 【leetcode】Maximum Subarray (53)

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

  7. 算法:寻找maximum subarray

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

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

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

  9. 【leetcode】Maximum Subarray

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

随机推荐

  1. 模板页 相对路径 JS 加载问题

    问题:我在master页面中引入了如下js文件:<script type="text/javascript" src="http://www.cnblogs.com ...

  2. CSS - 关于li中图文混排不能垂直居中的问题

    图片和文字一起放在li标签下不能同时垂直居中   解决办法: 1.设置图片的position:absolute; 2.把文字加上span标签: span{ height:30px;line-heigh ...

  3. Repeater里面加上if判断

    //创建时绑定 protected void ItemCreated(object sender, RepeaterItemEventArgs e) { if (e.Item.DataItem != ...

  4. log4j参数说明

    log4j.properties 使用 一.参数意义说明 输出级别的种类 ERROR.WARN.INFO.DEBUG ERROR 为严重错误 主要是程序的错误 WARN 为一般警告,比如session ...

  5. CSS网页元素居中

    1.水平居中:行内元素解决方案 只需要把行内元素包裹在一个属性display为block的父层元素中,并且把父层元素添加如下属性即可: text-align: center 适用元素:文字,链接,及其 ...

  6. 8、Khala的设备间管理+通信

    在之前的文档中,我们都是从单个设备的角度进行介绍,但在实际业务中,不同设备间存在交互行为.我们经常需要在一个设备的生命周期中查询另一个设备信息,或者向另一个设备进行通信.因此我们提供了设备管理模块来对 ...

  7. Python学习笔记4(函数与模块)

    1.Python程序的结构 Python的程序由包(package).模块(module)和函数组成. 模块是处理一类问题的集合,由函数和类组成. 包是由一系列模块组成的集合.包是一个完成特定任务的工 ...

  8. SharePoint solution and feature management with PowerShell

    /* Author: Jiangong SUN */ Hello, I want to introduce SharePoint solution and feature management usi ...

  9. [转]Cocos2d-x建工程时避免copy文件夹和库

    原文链接:  http://www.cnblogs.com/andyque/archive/2011/09/27/2192920.html 在上一篇教程中,我们演示了如何使用VS2010来新建一个工程 ...

  10. 异常处理与调试3 - 零基础入门学习Delphi52

    异常处理与调试3 让编程改变世界 Change the world by program 两种结构的嵌套 要在一个过程里同时实现处理异常和保护资源分配,关键要保证"try-except&qu ...