LeetCode: Maximum Subarray 解题报告
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 解题报告相关推荐
- [LeetCode] Maximum Subarray 最大子数组
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- LeetCode Maximum Product Subarray 解题报告
LeetCode 新题又更新了.求:最大子数组乘积. https://oj.leetcode.com/problems/maximum-product-subarray/ 题目分析:求一个数组,连续子 ...
- [LeetCode] Maximum Subarray
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- LeetCode Maximum Subarray
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- [LeetCode] Multiply Strings 解题报告
Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...
- LeetCode - Maximum Subarray
题目: Find the contiguous subarray within an array (containing at least one number) which has the larg ...
- LeetCode: Sort List 解题报告
Sort List Sort a linked list in O(n log n) time using constant space complexity. 使用Merge Sort, 空间复杂度 ...
- Leetcode Weekly 188 解题报告
文章目录 Leetcode 1441. 用栈操作构建数组 Leetcode 1442. 形成两个异或相等数组的三元组数目 Leetcode 1443. 收集树上所有苹果的最少时间 Leetcode 1 ...
- [LeetCode]Distinct Subsequences,解题报告
题目 Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequen ...
最新文章
- Redis 读写分离技术架构解析
- CVPR 2022 接收结果出炉!录用 2067 篇,接收数量上升24%(附最新论文速递)
- 生物信息通识技术研讨会
- vue 高阶面试题_高级Web前端工程师面试之Vue问题汇总解析
- AI学高数达到MIT本科水平,学了微积分线性代数概率论等6门课,不光能做题还能出题...
- JD 负载均衡中的 https
- AbstractListView源码分析8
- 科个普:进程、线程、并发、并行
- 今日测试:javascript笔试最常见的一道题
- 在linux系统下用rpm查看安装信息,rpm的查询命令
- 如何使用JS来开发室内地图商场停车场车位管理系统
- I.MX6 MAC地址修改
- 如何获取免费比特币?
- 背单词App-单词播放器10.31版本的原理
- 国家网信办《网络数据安全管理条例(征求意见稿)》为企业带来哪些新思考?
- php的ct表现,巨大垂体腺瘤(Pituitary adenoma)CT病例图片影像诊断分析
- 什么是全栈工程师?前端后端是做什么的?
- C++ 多种取整函数的使用和区别: ceil() floor() round() trunc() rint() nearbyint()
- reddit_Reddit如何设计和编码其详尽的愚人节体验,/ r / place
- JavaScript - 匿名函数具名化
热门文章
- server2008密码不满足密码策略的要求,检查最小密码长度、密码复杂性和密码历史的要求”的解决办法...
- Discuz! Ucenter API for JAVA jar包和测试代码
- Python将浏览器cookies共享给requests库
- [跟我学中小企业架构部署]之八:备份服务器部署
- ubuntu支持中文设置
- PHP(2):搭建PHP 运行环境中可能出现的问题及处理方法
- AAA Password Expiry in Cisco IOS Easy***
- 创建一个简单的ArcGIS Server ASP.NET网页
- 怎样才能有德国煤矿那样严密的安全网?
- final 和static的关系