当前位置:网站首页>#yyds干货盘点# 解决名企真题:连续最大和
#yyds干货盘点# 解决名企真题:连续最大和
2022-07-04 12:44:00 【51CTO】
1.简述:
描述
一个数组有 N 个元素,求连续子数组的最大和。 例如:[-1,2,1],和最大的连续子数组为[2,1],其和为 3
输入描述:
输入为两行。 第一行一个整数n(1 <= n <= 100000),表示一共有n个元素 第二行为n个数,即每个元素,每个整数都在32位int范围内。以空格分隔。
输出描述:
所有连续子数组中和最大的值。
示例1
输入:
输出:
2.代码实现:
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int sums=0, maxsums=Integer.MIN_VALUE; //考虑全为负数的情况
for(int i=0;i<n;i++){
sums+=sc.nextInt();
maxsums=Math.max(maxsums,sums);
sums= sums<0?0:sums; //代码核心了,如果当前求和为负,则抛弃之前的连续数组,重新开始求和
}
System.out.println(maxsums);
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
边栏推荐
- Runc hang causes the kubernetes node notready
- Etcd storage, watch and expiration mechanism
- Personalized online cloud database hybrid optimization system | SIGMOD 2022 selected papers interpretation
- FS4056 800mA充电ic 国产快充电源ic
- 光环效应——谁说头上有光的就算英雄
- Dry goods sorting! How about the development trend of ERP in the manufacturing industry? It's enough to read this article
- C语言课程设计题
- PostgreSQL 9.1 飞升之路
- Go zero micro service practical series (IX. ultimate optimization of seckill performance)
- 面向个性化需求的在线云数据库混合调优系统 | SIGMOD 2022入选论文解读
猜你喜欢

室外LED屏幕防水吗?

eclipse链接数据库中测试SQL语句删除出现SQL语句语法错误

诸神黄昏时代的对比学习

Interviewer: what is the difference between redis expiration deletion strategy and memory obsolescence strategy?

n++也不靠谱
Efficient! Build FTP working environment with virtual users

C#/VB. Net to add text / image watermarks to PDF documents
![[AI system frontier dynamics, issue 40] Hinton: my deep learning career and research mind method; Google refutes rumors and gives up tensorflow; The apotheosis framework is officially open source](/img/2c/b1d6277c1b23a6a77f90d5b2874759.png)
[AI system frontier dynamics, issue 40] Hinton: my deep learning career and research mind method; Google refutes rumors and gives up tensorflow; The apotheosis framework is officially open source
Three schemes to improve the efficiency of MySQL deep paging query

8个扩展子包!RecBole推出2.0!
随机推荐
求解:在oracle中如何用一条语句用delete删除两个表中jack的信息
Using scrcpy projection
C语言职工管理系统
Read the BGP agreement in 6 minutes.
Cors: standard scheme of cross domain resource request
Web知识补充
AI painting minimalist tutorial
Solution: how to delete the information of Jack in two tables with delete in one statement in Oracle
The only core indicator of high-quality software architecture
Use fail2ban to prevent password attempts
It is six orders of magnitude faster than the quantum chemical method. An adiabatic artificial neural network method based on adiabatic state can accelerate the simulation of dual nitrogen benzene der
Rsyslog configuration and use tutorial
XML入门一
【AI系统前沿动态第40期】Hinton:我的深度学习生涯与研究心法;Google辟谣放弃TensorFlow;封神框架正式开源
AI 绘画极简教程
A data person understands and deepens the domain model
使用Scrcpy投屏
C array supplement
Xue Jing, director of insight technology solutions: Federal learning helps secure the flow of data elements
Comprehensive evaluation of modular note taking software: craft, notation, flowus