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

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就可以

源码例如以下:

  1. public class Solution {
  2. public static int maxSubArray(int[] A) {
  3. if(A.length == 0)
  4. return 0;
  5. int max = A[0];
  6. for(int i = 0; i < A.length; i ++)
  7. if(A[i] > max)
  8. max = A[i];
  9. if(max <= 0)
  10. return max;
  11. int currentmax = 0;
  12. for(int i = 0;i < A.length; i ++){
  13. currentmax += A[i];
  14. if(currentmax <= 0){
  15. currentmax = 0;
  16. continue;
  17. }
  18. else{
  19. if(currentmax > max)
  20. max = currentmax;
  21. }
  22. }
  23. return max;
  24. }
  25. }

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. java与.net比较学习系列(3) 基本数据类型和类型转换

    在Java中,数据类型分为两类,一类是基本数据类型,另外一类是引用类型. 而在C#中,数据类型分为三类,分别是基元类型,值类型和引用类型.其中基元类型是.net framework框架中预定义的类型, ...

  2. PHP <<EOF EOF的使用方法

    PHP <<EOF EOF的使用方法 <?php     $name = '浅水游';     print <<<EOT             <html& ...

  3. CentOS6.5 --安装orale 11g(下)

    (7)     建立Oracle系统用户和安装目录 创建一个主组oinstall和一个副组dba.命令如下: groupadd oinstall groupadd dba 创建oracle安装文件 m ...

  4. [转] SQL Server游标的使用

    游标是邪恶的!        在关系数据库中,我们对于查询的思考是面向集合的.而游标打破了这一规则,游标使得我们思考方式变为逐行进行.对于类C的开发人员来着,这样的思考方式会更加舒服.        ...

  5. html验证码

    一.原理 1.在webservice服务端,新建一个Bitmap对象,将验证码字符串.干扰线和干扰点绘制到此Bitmap上——>转换为字节数组——>Base64字符串 2.<img ...

  6. PHP判断访客是否移动端浏览器访问

    今天要给大家分享一段PHP代码,该代码的功能是用来判断访客是否移动端浏览器访问,该功能的实现思路是通过HTTP_X_WAP_PROFILE. HTTP_VIA.HTTP_USER_AGENT等信息来判 ...

  7. dreamweaver批量去除空格,空行

    制作网店的时候,发现有空行,或者空格.如果数量比较少,可以手动清除.如果多的话,可以使用dreamweaver批量去除.方法如下: 第一步,用正则表达式删除所有空白行(其实这一步不一定要做,我只是为了 ...

  8. Myeclipse6.5配置反编译插件

    PS:jad.exe位置与Myeclipse6.5安装目录平行

  9. FreeBSD 安装axel提高ports的安装速度

    ########################  FreeBSD安装Ports ######################## 1 # ee /etc/portsnap.conf 设置SERVER ...

  10. 激活前一个程序(注册全局消息,使用Mutex探测,如果已经占用就广播消息通知第一个程序,然后第一个程序做出响应)

    unit MultInst; interface const MI_QUERYWINDOWHANDLE = ; MI_RESPONDWINDOWHANDLE = ; MI_ERROR_NONE = ; ...