当前位置:网站首页>#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.
边栏推荐
- eclipse链接数据库中测试SQL语句删除出现SQL语句语法错误
- CVPR 2022 | TransFusion:用Transformer进行3D目标检测的激光雷达-相机融合
- Reptile exercises (I)
- 用fail2ban阻止密码尝试攻
- JVM系列——栈与堆、方法区day1-2
- 比量子化学方法快六个数量级,一种基于绝热状态的绝热人工神经网络方法,可加速对偶氮苯衍生物及此类分子的模拟
- Meituan Ali's Application Practice on multimodal recall
- 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
- Optional values and functions of the itemized contenttype parameter in the request header
- 【FAQ】华为帐号服务报错 907135701的常见原因总结和解决方法
猜你喜欢
Reinforcement learning - learning notes 1 | basic concepts
实战:fabric 用户证书吊销操作流程
Building intelligent gray-scale data system from 0 to 1: Taking vivo game center as an example
Talk about the design and implementation logic of payment process
Zhongang Mining: in order to ensure sufficient supply of fluorite, it is imperative to open source and save flow
Three schemes to improve the efficiency of MySQL deep paging query
Practice: fabric user certificate revocation operation process
Flet教程之 03 FilledButton基础入门(教程含源码)(教程含源码)
Xue Jing, director of insight technology solutions: Federal learning helps secure the flow of data elements
Master the use of auto analyze in data warehouse
随机推荐
ASP.NET Core入门一
Etcd storage, watch and expiration mechanism
Introduction to XML I
Personalized online cloud database hybrid optimization system | SIGMOD 2022 selected papers interpretation
Oracle 被 Ventana Research 评为数字创新奖总冠军
Building intelligent gray-scale data system from 0 to 1: Taking vivo game center as an example
【云原生 | Kubernetes篇】深入了解Ingress(十二)
再说rsync+inotify实现数据的实时备份
Personalized online cloud database hybrid optimization system | SIGMOD 2022 selected papers interpretation
C foundation in-depth study I
8个扩展子包!RecBole推出2.0!
Is the outdoor LED screen waterproof?
爬虫练习题(一)
Cors: standard scheme of cross domain resource request
Dry goods sorting! How about the development trend of ERP in the manufacturing industry? It's enough to read this article
Commvault 和 Oracle 合作,在 Oracle 云上提供 Metallic数据管理即服务
面试官:Redis 过期删除策略和内存淘汰策略有什么区别?
使用宝塔部署halo博客
2022年中国移动阅读市场年度综合分析
提高MySQL深分页查询效率的三种方案