Maximum Subarray

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.

SOLUTION 1:

采用滑动窗口解决。sum 如果小于0,置为0,再加上当前值。

然后再与max相比,取大的。 1分钟AC

 1 public class Solution {
 2     public int maxSubArray(int[] A) {
 3         if (A == null || A.length == 0) {
 4             return 0;
 5         }
 6
 7         int max = Integer.MIN_VALUE;
 8         int sum = 0;
 9
10         int len = A.length;
11         for (int i = 0; i < len; i++) {
12             if (sum < 0) {
13                 sum = 0;
14             }
15
16             sum += A[i];
17             max = Math.max(max, sum);
18         }
19
20         return max;
21     }
22 }

View Code

https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/array/MaxSubArray_1220_2014.java

LeetCode: Maximum Subarray 解题报告相关推荐

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

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

  2. LeetCode Maximum Product Subarray 解题报告

    LeetCode 新题又更新了.求:最大子数组乘积. https://oj.leetcode.com/problems/maximum-product-subarray/ 题目分析:求一个数组,连续子 ...

  3. [LeetCode] Maximum Subarray

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

  4. LeetCode Maximum Subarray

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

  5. [LeetCode] Multiply Strings 解题报告

    Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...

  6. LeetCode - Maximum Subarray

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

  7. LeetCode: Sort List 解题报告

    Sort List Sort a linked list in O(n log n) time using constant space complexity. 使用Merge Sort, 空间复杂度 ...

  8. Leetcode Weekly 188 解题报告

    文章目录 Leetcode 1441. 用栈操作构建数组 Leetcode 1442. 形成两个异或相等数组的三元组数目 Leetcode 1443. 收集树上所有苹果的最少时间 Leetcode 1 ...

  9. [LeetCode]Distinct Subsequences,解题报告

    题目 Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequen ...

最新文章

  1. Redis 读写分离技术架构解析
  2. CVPR 2022 接收结果出炉!录用 2067 篇,接收数量上升24%(附最新论文速递)
  3. 生物信息通识技术研讨会
  4. vue 高阶面试题_高级Web前端工程师面试之Vue问题汇总解析
  5. AI学高数达到MIT本科水平,学了微积分线性代数概率论等6门课,不光能做题还能出题...
  6. JD 负载均衡中的 https
  7. AbstractListView源码分析8
  8. 科个普:进程、线程、并发、并行
  9. 今日测试:javascript笔试最常见的一道题
  10. 在linux系统下用rpm查看安装信息,rpm的查询命令
  11. 如何使用JS来开发室内地图商场停车场车位管理系统
  12. I.MX6 MAC地址修改
  13. 如何获取免费比特币?
  14. 背单词App-单词播放器10.31版本的原理
  15. 国家网信办《网络数据安全管理条例(征求意见稿)》为企业带来哪些新思考?
  16. php的ct表现,巨大垂体腺瘤(Pituitary adenoma)CT病例图片影像诊断分析
  17. 什么是全栈工程师?前端后端是做什么的?
  18. C++ 多种取整函数的使用和区别: ceil() floor() round() trunc() rint() nearbyint()
  19. reddit_Reddit如何设计和编码其详尽的愚人节体验,/ r / place
  20. JavaScript - 匿名函数具名化

热门文章

  1. server2008密码不满足密码策略的要求,检查最小密码长度、密码复杂性和密码历史的要求”的解决办法...
  2. Discuz! Ucenter API for JAVA jar包和测试代码
  3. Python将浏览器cookies共享给requests库
  4. [跟我学中小企业架构部署]之八:备份服务器部署
  5. ubuntu支持中文设置
  6. PHP(2):搭建PHP 运行环境中可能出现的问题及处理方法
  7. AAA Password Expiry in Cisco IOS Easy***
  8. 创建一个简单的ArcGIS Server ASP.NET网页
  9. 怎样才能有德国煤矿那样严密的安全网?
  10. final 和static的关系