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

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 OJ平台上Maximum Subarray题目O(n)复杂度解决方式

    原始题目例如以下,意为寻找数组和最大的子串,返回这个最大和就可以. Find the contiguous subarray within an array (containing at least ...

  2. LeetCode OJ平台上的Climbing Stairs题目用java迭代解决

    题目如下. You are climbing a stair case. It takes n steps to reach to the top. Each time you can either ...

  3. SAP云平台上的Low Code Development(低代码开发)解决方案

    Jerry之前曾经陆续写过一些文章,介绍SAP各种开发工具和开发平台: 那些年我用过的SAP IDE SAP智能机器人流程自动化解决方案 如何使用SAP Intelligent Robotic Pro ...

  4. Leetcode平台上的Median of Two Sorted Arrays题目用Java快排实现

    Leetcode平台上的Median of Two Sorted Arrays题目,大意就是找两个已排序数组的中位数.今天先用快排的方式实现一下,代码如下: There are two sorted ...

  5. [LeetCode] Maximum Subarray 最大子数组

    Find the contiguous subarray within an array (containing at least one number) which has the largest ...

  6. 【LeetCode】053. Maximum Subarray

    题目: Find the contiguous subarray within an array (containing at least one number) which has the larg ...

  7. Leetcode OJ: Maximun Subarray

    Maximum Subarray Find the contiguous subarray within an array (containing at least one number) which ...

  8. 【DP】LeetCode 53. Maximum Subarray

    LeetCode 53. Maximum Subarray Solution1:我的答案 动态规划 class Solution { public:int maxSubArray(vector< ...

  9. 【动态规划】LeetCode 53. Maximum Subarray

    LeetCode 53. Maximum Subarray 原题描述(求子序列最大和/最大子串):Find the contiguous subarray within an array (conta ...

最新文章

  1. [微信小程序]时间戳转日期
  2. 免费的新一代私有云平台Nano v0.3.1发布:云主机快照及媒体管理
  3. 简单BP网络识别数码表字符
  4. Python-解决粘包
  5. python词云有什么用_如何用Python实现词云效果
  6. 三层架构用户登录代码c语言,三层架构实现简单的用户登录代码
  7. c语言 void**类型转换,void *和其他指针的转化
  8. 【git】克隆成功,但是检出失败
  9. 如何使用 chrome devtool 分析前端代码执行耗时和内存占用
  10. Mac/Macbook Apple ID 无法登录:验证失败,发生未知错误
  11. Ubuntu中扩大虚拟机硬盘容量
  12. .pth.tar文件
  13. 文本框仅允许输入大于0的正整数
  14. WWW2023即将截稿
  15. 怎样建自己的网站?如何建立一个网站?建网站的步骤?
  16. python卷积神经网络预测股价_利用神经网络预测股票市场
  17. 用卡尔曼滤波器跟踪导弹(量测更新频率与时间更新频率不相等)
  18. 为什么数据库不适用于容器
  19. 深度优先与广度优先的思想
  20. 前端开发_HTML5_布局-流式布局

热门文章

  1. 语音问答机器人小程序
  2. 用html5颜色自动渐变,HTML5渐变背景色生成器
  3. B1:Unity制作Moba类游戏——Socket帧同步
  4. 考试分析评价JAVA_基于jsp的考试成绩分析-JavaEE实现考试成绩分析 - java项目源码...
  5. DOS 游戏编程二十一条
  6. pinyin4j把中文句子(含有多音字字母)转成拼音(二维数组递归求所有组合情况返回list)算法实现!
  7. c语言运算符与表达式ppt,C语言基本运算符与表达式.ppt
  8. java计算机毕业设计健身房管理系统源码+数据库+lw文档+系统
  9. ChatGPT的出圈与大佬们的焦虑
  10. go语言生成linux桌面快捷,使用Go语言在MacOS创建一个自定义的命令行工具