当前位置:网站首页>#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.
边栏推荐
- Xilinx/system-controller-c/boardui/ unable to connect to the development board, the solution of jamming after arbitrary operation
- 使用Scrcpy投屏
- 比量子化学方法快六个数量级,一种基于绝热状态的绝热人工神经网络方法,可加速对偶氮苯衍生物及此类分子的模拟
- Excuse me, have you encountered this situation? CDC 1.4 cannot use timestamp when connecting to MySQL 5.7
- Building intelligent gray-scale data system from 0 to 1: Taking vivo game center as an example
- C basic supplement
- 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
- XILINX/system-controller-c/BoardUI/无法连接开发板,任意操作后卡死的解决办法
- C#基础补充
- SQL语言
猜你喜欢
Alibaba cloud award winning experience: build a highly available system with polardb-x
Runc hang causes the kubernetes node notready
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
eclipse链接数据库中测试SQL语句删除出现SQL语句语法错误
When MDK uses precompiler in header file, ifdef is invalid
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
Annual comprehensive analysis of China's mobile reading market in 2022
Practice: fabric user certificate revocation operation process
ASP.NET Core入门一
Personalized online cloud database hybrid optimization system | SIGMOD 2022 selected papers interpretation
随机推荐
Introduction to XML II
C语言个人通讯录管理系统
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
FS4056 800mA充电ic 国产快充电源ic
Read the BGP agreement in 6 minutes.
Introduction to XML III
CVPR 2022 | TransFusion:用Transformer进行3D目标检测的激光雷达-相机融合
WPF double slider control and forced capture of mouse event focus
Personalized online cloud database hybrid optimization system | SIGMOD 2022 selected papers interpretation
Rsyslog configuration and use tutorial
XML入门三
请问大佬们有遇到这个情况吗,cdc 1.4 连接MySQL 5.7 无法使用 timestamp
mysql三级分销代理关系存储
Use fail2ban to prevent password attempts
2022KDD预讲 | 11位一作学者带你提前解锁优秀论文
Agile development / agile testing experience
n++也不靠谱
C language dormitory management query software
C foundation in-depth study I
Meituan Ali's Application Practice on multimodal recall