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. jquery模拟checkbox效果,以及background-size在jquery中的使用。

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http ...

  2. ArcEngine 添加字段

    private void AddField(IFeatureClass pFeatureClass, string name, string aliasName, esriFieldType Fiel ...

  3. mvc分页生成静态页,mvc生成静态页

    http://blog.csdn.net/xxj_jing/article/details/7899125 分页生成静态页 http://www.cnblogs.com/luanyilin/archi ...

  4. 关于找工作(二 Cover Letter)

    准备好了简历,下一个文档就是cover letter了.其实对衡量你是否是一个好的候选人来说,cover letter的作用几乎是零(很多情况下主管技术工作的人或者雇人经理根本见不到cover let ...

  5. PHP学习笔记三十七【http】

    <?php print_r($_SERVER); //$_SERVER预编译变量[数组]输出请求报文,注意大小写 echo "<br/>"; foreach($_ ...

  6. oracle用户权限的问题

    一.创建用户 create user username identified by password --username 创建的用户的名称 --password 创建的用户的密码 二.赋权限 gra ...

  7. Mybatis Generator最完整配置详解

    <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE generatorConfiguration ...

  8. MySQL 初学笔记 ① -- MySQL用户登录权限控制

    1. MySQL 登录 MySQL -u username -p 2. MySQL 创建用户 use mysql //进入mysql 表 INSERT INTO user (Host,User,Pas ...

  9. jQuery常用选择器汇总

    一.基本选择器 <body> <div> <div id="div1"> aaaaaaaaaaa</div> <div cla ...

  10. [Head First Python]5. 推导数据:处理数据

    读取4个文件内容,格式化数据,升序,显示每个文件前3个数据 julie.txt 2.59,2.11,2:11,2:23,3-10,2-23,3:10,3.21,3-21 james.txt 2-34, ...