LeetCode OJ平台上Maximum Subarray题目O(n)复杂度解决方式
原始题目例如以下,意为寻找数组和最大的子串,返回这个最大和就可以。
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)复杂度解决方式的更多相关文章
- LeetCode Array Easy 53. Maximum Subarray 个人解法 和分治思想的学习
Description Given an integer array nums, find the contiguous subarray (containing at least one numbe ...
- LeetCode练题——53. Maximum Subarray
1.题目 53. Maximum Subarray——Easy Given an integer array nums, find the contiguous subarray (containin ...
- 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 ...
- LeetCode之“动态规划”:Maximum Subarray
题目链接 题目要求: Find the contiguous subarray within an array (containing at least one number) which has t ...
- [LeetCode]题53:Maximum Subarray
Given an integer array nums, find the contiguous subarray (containing at least one number) which has ...
- LeetCode(53) Maximum Subarray
题目 Find the contiguous subarray within an array (containing at least one number) which has the large ...
- [LeetCode&Python] Problem 53. Maximum Subarray
Given an integer array nums, find the contiguous subarray (containing at least one number) which has ...
- 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 ...
- 【LeetCode算法-53】Maximum Subarray
Given an integer array nums, find the contiguous subarray (containing at least one number) which has ...
随机推荐
- 无心插柳OR志在必得?阿里推“来往”的意图
近年来,阿里巴巴在外围的动作确实不少,投资新浪微博.投资陌陌,配合阿里自身的一些战略调整,让人觉得这家公司似乎正在经历一场前所未有的“蜕变”.其实这也不难理解,在BAT三国演义中,任何一方都不 ...
- 内外连接、组函数、DDL、DML和TCL
前言 cross join ,是笛卡尔积:nature join 是自然连接. 正文 内外连接 inner join inner join 的inner能够省略. 内连接 在一个表中可以找到在还有一个 ...
- SQL Server -查看数据库扩展属性
1.fn_listextendedproperty 函数可以基于对象类型显示单个数据库对象或数据库中所有对象的扩展属性.例如,可以返回表或表中所有列的扩展属性. A.下面的示例显示了数据库本身设置的所 ...
- PHP学习笔记二十【静态方法】
<?php //静态变量的基本用法 //1,在类中定义变量 //2.定义方式[访问修饰符]static 变量名 //3.访问方式self::$变量名 第二种方式,类名::$变量名 //4.在类外 ...
- MyEclipse安装xfire插件
xfire因为过时,MyEclipse已经将其支,如果想用只能手动添加了. 安装步骤: 1.点击help┈┈┈→install from site出现下图 在第一栏里,输出http://dist.co ...
- hdu1860
#include<iostream> #include <stdio.h> #include<string> #include <iomanip> us ...
- ARM入门实践(一)----Mini6410上最简单的LED点灯裸机程序
Mini6410上最简单的LED点灯裸机程序 : 实验环境: 根据友善教程,要用ADS,据说现在都不用这个了,但是为了打开友善给的mcp工程,就下了一个,Win7下弄上兼容模式和管理员权限,再下一个S ...
- 南阳oj-喷水装置(一)
喷水装置(一) 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 现有一块草坪,长为20米,宽为2米,要在横中心线上放置半径为Ri的喷水装置,每个喷水装置的效果都会让以 ...
- C语言enum再学习
通常来说我们使用enum是这样的: enum week{ Mon, Tue, ... Sun }; enum week w; w = Mon; 这里默认Mon~Sun的值为0~6 也可以自己定值 , ...
- python笔记之ZipFile模块
python笔记之ZipFile模块 zipfile模块用来做zip格式编码的压缩和解压缩的,zipfile里有两个非常重要的class, 分别是ZipFile和ZipInfo, 在绝大多数的情况下, ...