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

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. UITabBar背景、icon图标颜色、被选中背景设置以及隐藏UITabBar的两种方式

    一.对UITabBar背景和icon图标的一些设置 (1)因为直接给UITabBar设置的背景颜色显示的不纯,半透明的感觉,所以,有时候我们可以直接利用纯色的图片作为背景达到想要的效果: (2)给ic ...

  2. 一个简单方法:构造xml的document,并将其转换为string

    首先,构造一个document对象: Document doc = null; try { doc = DocumentBuilderFactory.newInstance() .newDocumen ...

  3. [canvas]通过动态生成像素点做绚丽效果

    本例中的粒子就是实实在在的像素,由js代码在canvas上动态生成的像素点!这些像素点通过一个运动方法有规律地动了起来.透过这个思路,我们可以想到很多很炫的效果,但是这个性能有待考察.实验证明,动态控 ...

  4. iOS应用拨打电话

    方法一: 特点: 直接拨打, 不弹出提示. 并且, 拨打完以后, 留在通讯录中, 不返回到原来的应用. //拨打电话 - (void)callPhone:(NSString *)phoneNumber ...

  5. (转) Friendship and inheritance

    原地址: http://www.cplusplus.com/doc/tutorial/inheritance/ Friend functions In principle, private and p ...

  6. SqlServer日期查询

    一.sql server日期时间函数 Sql Server中的日期与时间函数 1.  当前系统日期.时间 select getdate() 2. dateadd  在向指定日期加上一段时间的基础上,返 ...

  7. [ofbiz]解决load-demo花费过长时间的问题

    一直以来使用公司配置的hp-cq45笔记本,在初始化ofbiz的时候load-demo需要花费很长一段时间,比如90分钟,或者129分钟. 解决办法:安装官方驱动--intel快速存储驱动 ok,lo ...

  8. What is an http upgrade?

    HTTP Upgrade is used to indicate a preference or requirement to switch to a different version of HTT ...

  9. C++之------构造函数

    创建一个对象时,常常需要作某些初始化的工作,例如对数据成员赋初值. 类的数据成员是不能在声明类时初始化的. class Time { public : //声明为公用成员 hour; minute; ...

  10. javascript之Error

    一.Error()构造函数 构造函数:new Error(); new Error(message); 二.Error.message //人类可读的错误消息 语法:error.message; 三. ...