当前位置:网站首页>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));
}
}
边栏推荐
- Regular Expression - Determine if a string matches the "AABB" pattern
- PDF不能打印和复制的问题如何解决?
- zip压缩包密码解密
- Error jinja2.exceptions.UndefinedError: 'form' is undefined
- 运算放大器(OPA)超详细参数讲解-运放---以及8个型号的运算放大器分析对比
- CMake中INSTALL_RPATH与BUILD_RPATH问题
- Use baidu EasyDL intelligent bin
- 报错:FSADeprecationWarning: SQLALCHEMY_TRACK_MODIFICATIONS重大开销和将disab补充道
- el-table缓存数据
- 数据库中间件-jdbi
猜你喜欢
How to find package information and pin definitions for NXP S32K1xx series microcontrollers
zip压缩包密码解密
工控设备的系统如何进行加固
带头双向循环链表的增删查改(C语言实现)
Unity backgammon game design and simple AI implementation (1)
缓存技术使用
Error jinja2.exceptions.UndefinedError: 'form' is undefined
中英文说明书丨CalBioreagents 山羊抗人白蛋白,IgG组分
Data center project preliminary summary
install flask
随机推荐
.NET高级技术
线程的6种状态
[R language] Normalize and organize files into folders of various file types
static静态关键字和继承
[GO]、数组与切片
APP商品详情源数据接口(淘宝/京东/拼多多/苏宁/抖音等平台详情数据分析接口)代码对接教程
Harbor Enterprise Mirror Warehouse Construction
Use of PlantUML plugin in idea
sql problem solving statement to create a table
逆向工程
运放-运算放大器经典应用电路大全-应用电路大全
Gao Zelong, a famous digital collection expert and founder of the Digital Collection Conference, was interviewed by China Entrepreneur Magazine
PDF不能打印和复制的问题如何解决?
代码目录结构
运算放大器(OPA)超详细参数讲解-运放---以及8个型号的运算放大器分析对比
idea中PlantUML插件使用
Fragments
C language implements sequential stack and chain queue
C语言实现顺序栈和链队列
db.sqlite3 has no "as Data Source" workaround