当前位置:网站首页>剑指 Offer 42. 连续子数组的最大和
剑指 Offer 42. 连续子数组的最大和
2022-06-24 19:40:00 【我不是萧海哇~~~~】
输入一个整型数组,数组中的一个或连续多个整数组成一个子数组。求所有子数组的和的最大值。要求时间复杂度为O(n)。
示例1:
输入: nums = [-2,1,-3,4,-1,2,1,-5,4]
输出: 6
解释: 连续子数组 [4,-1,2,1] 的和最大,为 6。
提示:
- 1 <= arr.length <= 10^5
- -100 <= arr[i] <= 100
Code:
int maxSubArray(vector<int>& nums)
{
int maxnum=nums[0];
int sum=0;
for(int i=0;i<nums.size();i++)
{
sum=max(sum+nums[i],nums[i]);
maxnum=max(maxnum,sum);
}
cout<<maxnum<<endl;
return maxnum;
}
边栏推荐
- Data communication foundation - Ethernet port mirroring and link aggregation
- How to compare two or more distributions: a summary of methods from visualization to statistical testing
- Wechat side: what is consistent hash? In what scenario? What problems have been solved?
- 电力系统| IEEE论文投稿流程
- 【Mongodb】READ_ ME_ TO_ RECOVER_ YOUR_ Data, the database is deleted maliciously
- Analyze the implementation process of oauth2 distributed authentication and authorization based on the source code
- Beijiafu (p+f) R2000 modified radar IP
- Leetcode algorithm The first common node of two linked lists
- 堆內存分配的並發問題
- Parental delegation mechanism
猜你喜欢

Solution to the login error of tangdou people

【Mongodb】READ_ ME_ TO_ RECOVER_ YOUR_ Data, the database is deleted maliciously

Fanuc robot_ Introduction to Karel programming (1)

Nuscenes -- remedies for missing image files or 0-size images encountered during dataset configuration

Online filing process
![[personal experiment report]](/img/04/c9e1bee19bff9d55b73c531f7b17f4.png)
[personal experiment report]

2022-06-10 工作记录--JS-获取到某一日期N天后的日期

倍加福(P+F)R2000修改雷达IP

【文本数据挖掘】中文命名实体识别:HMM模型+BiLSTM_CRF模型(Pytorch)【调研与实验分析】

2022安全员-B证考试题库及答案
随机推荐
Kubevela v1.2 release: the graphical operation console velaux you want is finally here
重磅!法大大上榜“专精特新”企业
证件照处理
Nuscenes -- remedies for missing image files or 0-size images encountered during dataset configuration
Database transaction Transanction
seven
Learn more about the practical application of sentinel
环境配置 | VS2017配置OpenMesh源码和环境
Use of selector for NiO multiplexing
动态菜单,自动对齐
Win10 or win11 printer cannot print
2022年安全员-A证考题及答案
AQS source code analysis
详细了解关于sentinel的实际应用
Basic principles of spanning tree protocol
倍加福(P+F)R2000修改雷达IP
Yyds dry goods inventory junit5 learning II: assumptions class
Redis hop table
【軟件工程】期末重點
C language operators and expressions