当前位置:网站首页>53. Maximum subarray sum: give you an integer array num, please find a continuous subarray with the maximum sum (the subarray contains at least one element) and return its maximum sum.
53. Maximum subarray sum: give you an integer array num, please find a continuous subarray with the maximum sum (the subarray contains at least one element) and return its maximum sum.
2022-07-05 13:21:00 【? abc!】
Title Description
Give you an array of integers nums , Please find a continuous subarray with the largest sum ( A subarray contains at least one element ), Return to its maximum and .
Subarray Is a continuous part of the array .
Example 1:
Input :nums = [-2,1,-3,4,-1,2,1,-5,4]
Output :6
explain : Continuous subarray [4,-1,2,1] And the biggest , by 6 .
Example 2:
Input :nums = [1]
Output :1
Example 3:
Input :nums = [5,4,-1,7,8]
Output :23
Tips :
1 <= nums.length <= 105
-104 <= nums[i] <= 104
Ideas
Using the idea of dynamic programming :
- Look directly at the code , The first mock examination clear
Code
class Solution {
public int maxSubArray(int[] nums) {
// initialization
//pre It means after adding so far , Maximum value
//maxAns Represents the maximum value of continuous addition
int pre = 0, maxAns = nums[0];
// Circular array
for(int x : nums){
// Current value , Compare with the result of adding the current value and the previous value
pre = Math.max(pre + x, x);
// Get the maximum value so far
maxAns = Math.max(maxAns,pre);
}
return maxAns;
}
}
Code instructions
Note see ...
边栏推荐
- Le rapport de recherche sur l'analyse matricielle de la Force des fournisseurs de RPA dans le secteur bancaire chinois en 2022 a été officiellement lancé.
- Matlab paper chart standard format output (dry goods)
- Write macro with word
- 私有地址有那些
- 事务的基本特性和隔离级别
- Realize the addition of all numbers between 1 and number
- Solve Unicode decodeerror: 'GBK' codec can't decode byte 0xa2 in position 107
- Sorry, we can't open xxxxx Docx, because there is a problem with the content (repackaging problem)
- OpenHarmony应用开发之Navigation组件详解
- MATLAB论文图表标准格式输出(干货)
猜你喜欢
![[notes of in-depth study paper]uctransnet: rethink the jumping connection in u-net from the perspective of transformer channel](/img/b6/f9da8a36167db10c9a92dabb166c81.png)
[notes of in-depth study paper]uctransnet: rethink the jumping connection in u-net from the perspective of transformer channel

Developers, is cloud native database the future?

MSTP and eth trunk
![[notes of in-depth study paper]transbtsv2: wider instead of deep transformer for medical image segmentation](/img/70/6de0346df8527af6c88db1ff89947b.png)
[notes of in-depth study paper]transbtsv2: wider instead of deep transformer for medical image segmentation

How to protect user privacy without password authentication?

DataPipeline双料入选中国信通院2022数智化图谱、数据库发展报告

【每日一题】1200. 最小绝对差

私有地址有那些

RHCSA9

About the single step debugging of whether SAP ui5 floating footer is displayed or not and the benefits of using SAP ui5
随机推荐
Flutter InkWell & Ink组件
Reflection and imagination on the notation like tool
Natural language processing from Xiaobai to proficient (4): using machine learning to classify Chinese email content
运筹说 第68期|2022年最新影响因子正式发布 快看管科领域期刊的变化
Hiengine: comparable to the local cloud native memory database engine
Get you started with Apache pseudo static configuration
事务的基本特性和隔离级别
【服务器数据恢复】某品牌服务器存储raid5数据恢复案例
Difference between avc1 and H264
Simple page request and parsing cases
STM32 and motor development (from architecture diagram to documentation)
初次使用腾讯云,解决只能使用webshell连接,不能使用ssh连接。
CloudCompare——点云切片
前缀、中缀、后缀表达式「建议收藏」
数据泄露怎么办?'华生·K'7招消灭安全威胁
今年上半年,通信行业发生了哪些事?
53. 最大子数组和:给你一个整数数组 nums ,请你找出一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和。
Put functions in modules
【Hot100】33. 搜索旋转排序数组
Fragmented knowledge management tool memos