当前位置:网站首页>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));
}
}
边栏推荐
- db.sqlite3没有“as Data Source“解决方法
- 6 states of a thread
- kubernetes security
- CMake中INSTALL_RPATH与BUILD_RPATH问题
- Import the pycharm environment package into another environment
- A test engineer with an annual salary of 35W was laid off. Personal experience: advice that you have to listen to
- 数据库中间件-jdbi
- Unity 五子棋游戏设计和简单AI(3)
- Can Jincang Database Set Transaction Automatic Commit?
- .NET高级技术
猜你喜欢
2022-08-08: Given an array arr, it represents the height of the missiles that will appear in order from morning to night.When the cannon shoots missiles, once the cannon is set to shoot at a certain h
Search 1688 product interface by image (item_search_img-search 1688 product by image (Politao interface) code docking tutorial
[email protected]@BSABiS nanoparticles) | dendrimer-stabilized bismuth sulfide nanop"/>
Ferric oxide/bismuth sulfide nanocomposites ([email protected]@BSABiS nanoparticles) | dendrimer-stabilized bismuth sulfide nanop
redis 运行lua 脚本 出现Invalid argument(s)
Word文件的只读模式没有密码怎么退出?
PDF不能打印和复制的问题如何解决?
按图搜索1688商品接口(item_search_img-按图搜索1688商品(拍立淘接口)代码对接教程
Unity 五子棋游戏设计和简单AI(2)
运放-运算放大器经典应用电路大全-应用电路大全
推进产教融合 赋能教育创新发展 | 华云数据荣获“企业贡献奖”
随机推荐
How to automatically fill down an excel table without dragging the mouse down
BeautifulSoup4的介绍与使用
[R language] Extract all files under a folder to a specific folder
工控设备的系统如何进行加固
Remember a nest.js route that matches all the path problems that follow
golang zip aes base64
String.toLowerCase(Locale.ROOT)
Teach you how to make the Tanabata meteor shower in C language - elegant and timeless (detailed tutorial)
pdf加密、找回密码
2022.8.8DAY628
Fragments
中英文说明书丨CalBioreagents 山羊抗人白蛋白,IgG组分
输入框最前面添加放大镜&&background-image和background-color冲突问题
Online tool for sorting multi-line strings
vs番茄助手的方便功能和便捷快捷键介绍
Unity五子棋游戏设计 和简单AI实现(1)
Go lang1.18入门精炼教程——第一章:环境搭建
Qt learning (3) - Qt module
Unity 五子棋游戏设计和简单AI(2)
[HNOI2002]营业额统计