当前位置:网站首页>#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.
边栏推荐
- CVPR 2022 | TransFusion:用Transformer进行3D目标检测的激光雷达-相机融合
- 上汽大通MAXUS正式发布全新品牌“MIFA”,旗舰产品MIFA 9正式亮相!
- Scripy framework learning
- 8个扩展子包!RecBole推出2.0!
- C language dormitory management query software
- Oracle was named the champion of Digital Innovation Award by Ventana research
- .NET 使用 redis
- ASP.NET Core入门一
- Runc hang causes the kubernetes node notready
- Node mongodb installation
猜你喜欢

Master the use of auto analyze in data warehouse

After the game starts, you will be prompted to install HMS core. Click Cancel, and you will not be prompted to install HMS core again (initialization failure returns 907135003)

Alibaba cloud award winning experience: build a highly available system with polardb-x
Efficient! Build FTP working environment with virtual users
![[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

PostgreSQL 9.1 soaring Road

HAProxy高可用解决方案

2022年中国移动阅读市场年度综合分析

求解:在oracle中如何用一条语句用delete删除两个表中jack的信息

Personalized online cloud database hybrid optimization system | SIGMOD 2022 selected papers interpretation
随机推荐
C语言图书租赁管理系统
一个数据人对领域模型理解与深入
从0到1建设智能灰度数据体系:以vivo游戏中心为例
WPF双滑块控件以及强制捕获鼠标事件焦点
请问大佬们有遇到这个情况吗,cdc 1.4 连接MySQL 5.7 无法使用 timestamp
Practice: fabric user certificate revocation operation process
用fail2ban阻止密码尝试攻
CVPR 2022 | TransFusion:用Transformer进行3D目标检测的激光雷达-相机融合
Alibaba cloud award winning experience: build a highly available system with polardb-x
C language dormitory management query software
【云原生 | Kubernetes篇】深入了解Ingress(十二)
XML入门一
光环效应——谁说头上有光的就算英雄
remount of the / superblock failed: Permission denied
Building intelligent gray-scale data system from 0 to 1: Taking vivo game center as an example
SQL语言
In 2022, it will be es2022 soon. Do you only know the new features of ES6?
Cann operator: using iterators to efficiently realize tensor data cutting and blocking processing
C语言集合运算
8个扩展子包!RecBole推出2.0!