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 ...
随机推荐
- K贪心
<span style="color:#330099;">/* K - 贪心 基础 Time Limit:1000MS Memory Limit:32768KB 64b ...
- Spring项目的建立-移植流程(非入门教程)
Creat by Zhou yong in 2016/4/15/19:00 jar包 java 1.7 tomcat的两个jar包 2个文件上传的jar包 json支持的jar包 hibernate- ...
- javascript学习笔记(一)
学习书籍 Javascript高级程序设计 第3,4章 javascript数据类型 Undefined(undefined) Null(null,空指针) Boolean(true,false) N ...
- OpenCV——Sobel和拉普拉斯变换
Sobel变换和拉普拉斯变换都是高通滤波器. 什么是高通滤波器呢?就是保留图像的高频分量(变化剧烈的部分),抑制图像的低频分量(变化缓慢的部分).而图像变化剧烈的部分,往往反应的就是图像的边沿信息了. ...
- 跨平台渲染框架尝试 - constant buffer的管理
1. Preface Constant buffer是我们在编写shader的时候,打交道最多的一种buffer resource了.constant表明了constant buffer中的数据,在一 ...
- hibernate操作数据库总结(转)
一.query方式 1.hibernate使用原生态的sql语句执行数据库查询 有些时候有些开发人员总觉得用hql语句不踏实,程序出现了错误,就猜测因为不是原生态的sql语句,数据库不支持,因此情愿选 ...
- css选择表格偶数行
css代码tr:nth-child(even){background:gray} 选择偶数行 tr:nth-child(even){background:gray} 选择奇数行
- iOS Plist NSUserDefaults 数据存储
@import url(http://i.cnblogs.com/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/c ...
- Html 编码 queryUrl = encodeURI(queryUrl);
Html 编码 queryUrl = encodeURI(queryUrl);
- [WPF]ListView点击列头排序功能实现
[转] [WPF]ListView点击列头排序功能实现 这是一个非常常见的功能,要求也很简单,在Column Header上显示一个小三角表示表示现在是在哪个Header上的正序还是倒序就可以了. ...