当前位置:网站首页>P7阿里面试题2020.07 之滑动窗算法(阿里云面试)
P7阿里面试题2020.07 之滑动窗算法(阿里云面试)
2022-08-09 06:29:00 【史上最强的弟子】
题目是有一个数据之中的数字可能为正负整数,求连续n个数之和的最大值。
其实就是双指针滑动记录循环中的最大值。
实现:
import java.math.BigDecimal;
public class Test9 {
/*public double test(Double x,int flage){
double returnData = 0.0;
double x1 = 0;
double x2 = x;
double point = ;
while(new BigDecimal(x).)
return returnData;
}*/
/*public int test(int[] arr){
int maxtotal = 0;
for(int i = 0;i<arr.length;i++){
int sumTotal =arr[i];
int sumTotalMax = 0;
for(int j = i+1;j<arr.length;j++){
if(sumTotal+arr[j]>sumTotalMax){
sumTotalMax = sumTotal+arr[j];
}
sumTotal=sumTotal+arr[j];
}
if(sumTotalMax>maxtotal){
maxtotal = sumTotalMax;
}
}
return maxtotal;
}*/
public int test(int[] arr){
int maxtotal = 0;
for(int i = 0;i<arr.length;i++){
int sumTotal =arr[i];
int sumTotalMax = 0;
//这里做了一个判断判断的依据是前一个数是整数,后一个数是负数
if(i+1<arr.length&&sumTotal+arr[i+1]<arr[i+1]){
continue;
}
for(int j = i+1;j<arr.length;j++){
if(sumTotal+arr[j]>sumTotalMax){
sumTotalMax = sumTotal+arr[j];
}
sumTotal=sumTotal+arr[j];
}
if(sumTotalMax>maxtotal){
maxtotal = sumTotalMax;
}
}
return maxtotal;
}
public static void main(String[] args) {
Test9 test9 = new Test9();
int[] arr = {-2,11,-4,13,-5,2};
System.out.println(test9.test(arr));
}
}
边栏推荐
- static静态关键字和继承
- 05 多线程与高并发 - ThreadPoolExecutor 源码解析
- install flask
- flask创建数据库失败未报错
- 简单工厂模式
- zip压缩包密码解密
- Silently start over, the first page is also a new page
- 为什么以太网无法接收大于1500字节的数据包?
- Magnetic Core-Shell Fe3O4 Particles Supported Gold Nanostars | Magnetic Fe3O4-POSS-COOH | Superparamagnetic Fe3O4-Polydopamine Core-Shell Nanoparticles
- Unity backgammon game design and simple AI implementation (1)
猜你喜欢

输入框最前面添加放大镜&&background-image和background-color冲突问题

工控设备的系统如何进行加固

redis 运行lua 脚本 出现Invalid argument(s)

workbench 数据导出
![[MySQL] Second, the relationship between processes, MySQL password cracking, table building and database building related commands](/img/20/a0fb44e9360837146d0ed696c9e992.png)
[MySQL] Second, the relationship between processes, MySQL password cracking, table building and database building related commands

e-learning summary

Go lang1.18入门精炼教程——第一章:环境搭建

db.sqlite3 has no "as Data Source" workaround

Output method of list string print(*a) print(““.join(str(c) for c in a) )

Teach you how to make the Tanabata meteor shower in C language - elegant and timeless (detailed tutorial)
随机推荐
Unity 五子棋游戏设计和简单AI(2)
IQ Products巨细胞病毒CMV感染检测试剂盒的特征和应用
db.sqlite3没有“as Data Source“解决方法
GNNExplainer应用于节点分类任务
Xilinx Zynq ZynqMP DNA
Introduction and use of BeautifulSoup4
详解C语言中的wait()函数和waitpid()函数
Error jinja2.exceptions.UndefinedError: 'form' is undefined
Magnetic Core-Shell Fe3O4 Particles Supported Gold Nanostars | Magnetic Fe3O4-POSS-COOH | Superparamagnetic Fe3O4-Polydopamine Core-Shell Nanoparticles
程序性能分析 —— 复杂度分析
中英文说明书丨TRC 交替醇(Catalogue NumberA575760)
CalBioreagents超全Id 蛋白兔单克隆抗体,助力科研
redis 运行lua 脚本 出现Invalid argument(s)
io.lettuce.core.RedisCommandTimeoutException Command timed out
kubernetes security
phpstudy install flarum forum
Use of PlantUML plugin in idea
[R language] Extract all files under a folder to a specific folder
Excel受保护的工作表怎么操作?
Getting started with kubernetes apparmor