当前位置:网站首页>#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.
边栏推荐
- Apache服务器访问日志access.log设置
- C语言宿舍管理查询软件
- Personalized online cloud database hybrid optimization system | SIGMOD 2022 selected papers interpretation
- 高效!用虚拟用户搭建FTP工作环境
- Besides, rsync+inotify realizes real-time backup of data
- Configure WebDAV server on Apache
- 7 月数据库排行榜:MongoDB 和 Oracle 分数下降最多
- Oracle was named the champion of Digital Innovation Award by Ventana research
- 【AI系统前沿动态第40期】Hinton:我的深度学习生涯与研究心法;Google辟谣放弃TensorFlow;封神框架正式开源
- Web知识补充
猜你喜欢
Golang sets the small details of goproxy proxy proxy, which is applicable to go module download timeout and Alibaba cloud image go module download timeout
提高MySQL深分页查询效率的三种方案
2022年中国移动阅读市场年度综合分析
Three schemes to improve the efficiency of MySQL deep paging query
Database lock table? Don't panic, this article teaches you how to solve it
求解:在oracle中如何用一条语句用delete删除两个表中jack的信息
CVPR 2022 | TransFusion:用Transformer进行3D目标检测的激光雷达-相机融合
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
7 月数据库排行榜:MongoDB 和 Oracle 分数下降最多
Alibaba cloud award winning experience: build a highly available system with polardb-x
随机推荐
Introduction to XML II
《预训练周刊》第52期:屏蔽视觉预训练、目标导向对话
Configure WebDAV server on Apache
WPF double slider control and forced capture of mouse event focus
XML入门二
Cors: standard scheme of cross domain resource request
用fail2ban阻止密码尝试攻
实战:fabric 用户证书吊销操作流程
C语言个人通讯录管理系统
Simple understanding of binary search
Xilinx/system-controller-c/boardui/ unable to connect to the development board, the solution of jamming after arbitrary operation
Efficient! Build FTP working environment with virtual users
面向个性化需求的在线云数据库混合调优系统 | SIGMOD 2022入选论文解读
Web knowledge supplement
Comparative study of the gods in the twilight Era
C语言职工管理系统
阿里云有奖体验:用PolarDB-X搭建一个高可用系统
Cann operator: using iterators to efficiently realize tensor data cutting and blocking processing
提高MySQL深分页查询效率的三种方案
Rsyslog configuration and use tutorial