当前位置:网站首页>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 ...
边栏推荐
- Small case of function transfer parameters
- 先写API文档还是先写代码?
- STM32 and motor development (from architecture diagram to documentation)
- 事务的基本特性和隔离级别
- 记录一下在深度学习-一些bug处理
- Laravel document reading notes -mews/captcha use (verification code function)
- Cf:a. the third three number problem
- Shandong University Summer Training - 20220620
- 私有地址有那些
- Although the volume and price fall, why are the structural deposits of commercial banks favored by listed companies?
猜你喜欢
Put functions in modules
Laravel document reading notes -mews/captcha use (verification code function)
[深度学习论文笔记]UCTransNet:从transformer的通道角度重新思考U-Net中的跳跃连接
About the single step debugging of whether SAP ui5 floating footer is displayed or not and the benefits of using SAP ui5
Fragmented knowledge management tool memos
潘多拉 IOT 开发板学习(HAL 库)—— 实验7 窗口看门狗实验(学习笔记)
Write API documents first or code first?
Flutter draws animation effects of wave movement, curves and line graphs
Principle and configuration of RSTP protocol
运筹说 第68期|2022年最新影响因子正式发布 快看管科领域期刊的变化
随机推荐
国际自动机工程师学会(SAE International)战略投资几何伙伴
Rocky basics 1
Go pointer
Cf:a. the third three number problem
C object storage
C# 对象存储
FPGA 学习笔记:Vivado 2019.1 添加 IP MicroBlaze
STM32 and motor development (from architecture diagram to documentation)
OpenHarmony应用开发之Navigation组件详解
什么是网络端口
Reverse Polish notation
《2022年中国银行业RPA供应商实力矩阵分析》研究报告正式启动
Android本地Sqlite数据库的备份和还原
数据湖(七):Iceberg概念及回顾什么是数据湖
Navigation property and entityset usage in SAP segw transaction code
Discussion on error messages and API versions of SAP ui5 getsaplogonlanguage is not a function
How to realize batch sending when fishing
53. 最大子数组和:给你一个整数数组 nums ,请你找出一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和。
解决uni-app配置页面、tabBar无效问题
Talk about seven ways to realize asynchronous programming