原始题目例如以下,意为寻找数组和最大的子串,返回这个最大和就可以。

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.

最普通的方法就是两层循环来寻找,复杂度为O(n^2).

在木易先森的指导下,有一个极其简单的O(n)复杂度的方法:

-    先找出数组max值,假设max小于0 ,啥也别说了,直接返回max.

-    设置辅助变量currentmax=0;数组从头至尾扫描一遍

-    假设currentmax + A[i] <= 0,意味着这个子串对于我们寻找最大和是没有不论什么帮助的,此时,直接再置currentmax = 0

-    假设currentmax + A[i] > 0,意味着这个子串的和是有意义的,将这个和跟max比較,时刻保持max的值是当前最理想的最大值

-    最后返回max就可以

源码例如以下:

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

LeetCode OJ平台上Maximum Subarray题目O(n)复杂度解决方式的更多相关文章

  1. LeetCode Array Easy 53. Maximum Subarray 个人解法 和分治思想的学习

    Description Given an integer array nums, find the contiguous subarray (containing at least one numbe ...

  2. LeetCode练题——53. Maximum Subarray

    1.题目 53. Maximum Subarray——Easy Given an integer array nums, find the contiguous subarray (containin ...

  3. LeetCode OJ 209. Minimum Size Subarray Sum

    Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...

  4. LeetCode之“动态规划”:Maximum Subarray

    题目链接 题目要求: Find the contiguous subarray within an array (containing at least one number) which has t ...

  5. [LeetCode]题53:Maximum Subarray

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

  6. LeetCode(53) Maximum Subarray

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

  7. [LeetCode&Python] Problem 53. Maximum Subarray

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

  8. LeetCode OJ:Minimum Size Subarray Sum(最小子数组的和)

    Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...

  9. 【LeetCode算法-53】Maximum Subarray

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

随机推荐

  1. Cocos2d-x 3.0rc0版本号项目的创建和部署

    <1>执行setup.py 依照提示.加入你须要加入的环境变量 <2>创建项目批处理文件Create-Cocos2d-x 3.0-Project.bat 内容例如以下: @ec ...

  2. js 代码命名规范系列

    在微博上看到一个段子 “老子哪天出任ceo迎娶白富美走上人生巅峰之后,一定要雇两个长腿大熊的妹子.一个帮我想变量名字,一个帮我想git commit的message!” 可以看出 命名方方面面的问题困 ...

  3. GDI+创建Graphics对象的2种方式

      1.this.CreateGraphics()     // 调用控件的CreateGraphics()方法 2.在OnPaint事件中,PaintEventArgs类型的参数e对象的Graphi ...

  4. Android HttpClient框架get和post方式提交数据(非原创)

    1.fragment_main.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android& ...

  5. sql 模糊查询带下划线的字段 _

    1.SELECT * FROM dbo.tb_Test 2.SELECT * FROM dbo.tb_Test WHERE name LIKE '%c_%' 3.SELECT * FROM dbo.t ...

  6. Autofac创建实例的方法总结 【转】

    Autofac创建实例的方法总结   1.InstancePerDependency 对每一个依赖或每一次调用创建一个新的唯一的实例.这也是默认的创建实例的方式. 官方文档解释:Configure t ...

  7. MagicalRecord(简化CoreData操作)

    1.新建项目不勾选coredata 2.pod 'MagicalRecord' 3.新建模型文件 4.添加实体和属性 5.Create NSManagedObject subclass 6.增 Per ...

  8. 截取NSString字符串

    NSString类中提供了这样三个方法用于获取子字符串: – substringFromIndex: – substringWithRange: – substringToIndex: 具体的使用见下 ...

  9. 虚拟机VirtualBox和Ubutu

    虚拟机的作用1. 演示环境,可以安装各种演示环境,便于做各种例子: 2. 保证主机的快速运行,减少不必要的垃圾安装程序,偶尔使用的程序,或者测试用的程序在虚拟机上运行: 3. 避免每次重新安装,银行等 ...

  10. C++Primer笔记(1)

    1.初始化 在C++中,初始化与赋值操作是完全不同的两个操作.初始化不是赋值,初始化的含义是创建变量时赋予其一个初始值,而赋值的含义是把对象的当前值擦除,而以一个新值来代替. 初始化的方式有: ; } ...