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

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. 移动支付之智能IC卡与Android手机进行NFC通信

    本文来自http://blog.csdn.net/hellogv/ .引用必须注明出处.        眼下常见的智能IC卡执行着JavaCard虚拟机.智能IC卡上能够执行由精简后的Java语言编写 ...

  2. .net 4.5 新特性 async await 一般处理程序实例

    using System; using System.Collections.Generic; using System.Linq; using System.Threading; using Sys ...

  3. Android编译过程详解(二)

    通过上篇文章,我们分析了编译android时source build/envsetup.sh和lunch命令,在执行完上述两个命令后, 我们就可以进行编译android了. 1. make  执行ma ...

  4. Qt Creator下载

    Qt官网 http://qt.digia.com qt的历史版本可以到http://download.qt-project.org/archive/qt/下载

  5. js特殊字符转义

    点的转义:. ==> \\u002E 美元符号的转义:$ ==> \\u0024 乘方符号的转义:^ ==> \\u005E 左大括号的转义:{ ==> \\u007B 左方括 ...

  6. export-data.js

    var timeBtnClick = (function() { function _todayClick() { $('.select-time .today').on('click', funct ...

  7. ajax创建对象

    <script>     function createAjax(){         var request=false;                   //window对象中有X ...

  8. Ubunu下安装Docker

    安装Docker步骤如下: sudo apt-get update sudo apt-get install apt-transport-https sudo apt-key adv --keyser ...

  9. poj 1036 Gangsters

    http://poj.org/problem?id=1036 题意:N个土匪,伸缩门的范围是K, 时间T, 伸缩门在[0, k]范围内变动,每个单位时间可以不变伸长或者缩短一个单位.给出每个最烦到达的 ...

  10. Linux安装开发环境,必须配置的环节(Fedora15版本)

    前提:U盘安装fedora:<[原]U盘安装Fedora15 DVD镜像>.<Grub引导安装Fedora15>   1.设置代理上网:<fedora 配置网络代理> ...