当前位置:网站首页>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));
}
}
边栏推荐
- 【Feel】Unity Feel插件中,Camera无法正确显示CameraShake
- Unity 五子棋游戏设计和简单AI(2)
- redis 运行lua 脚本 出现Invalid argument(s)
- Unity backgammon game design and simple AI implementation (1)
- Inception V3 闭眼检测
- Use of PlantUML plugin in idea
- 一道很简答但是没答对的SQL题
- 思维方法 解决问题的能力
- BeautifulSoup4的介绍与使用
- Invalid argument(s) appears when redis runs lua script
猜你喜欢
qt send mail program
普罗米修斯原理及节点发布
深度学习-神经网络原理2
Data center project preliminary summary
GNNExplainer应用于节点分类任务
How to automatically fill down an excel table without dragging the mouse down
Unity backgammon game design and simple AI implementation (1)
SiO2 / KH550 modified ferroferric oxide nano magnetic particles | PDA package the ferromagnetic oxide nanoparticles (research)
6 states of a thread
C language implements sequential stack and chain queue
随机推荐
pdf加密、找回密码
如何操作数据库
Program Performance Analysis - Complexity Analysis
Search 1688 product interface by image (item_search_img-search 1688 product by image (Politao interface) code docking tutorial
e-learning summary
shardingsphere数据分片配置项说明和示例
简单工厂模式
static静态关键字和继承
sql problem solving statement to create a table
install flask
力扣刷题180
Online tool for sorting multi-line strings
Used to import the data type
6 states of a thread
qt send mail program
语句加锁分析
使用百度EasyDL实现智能垃圾箱
el-table缓存数据
CalBioreagents超全Id 蛋白兔单克隆抗体,助力科研
代码目录结构