平均分割数组
来源:互联网 发布:卓智网络破解 编辑:程序博客网 时间:2024/05/20 06:56
阿里笔试题,平均分割数组
对于一个长度为N的整形数组A,数组里所有的数都是正整数,对于两个满足 0<=X<=Y< N的整数,A[X],A[X+1]…A[y]构成A的一个切片,记作(X,Y)。
用三个下标m1,m2,m3下标满足条件 0< m1,m1+1< m2,m2+1< m3< N-1。
可以把这个整型数组分成(0,m1-1),(m1+1,m2-1),(m2+1,m3-1),(m3+1,N-1)四个切片。如果这四个切片中的整数求和相等,称作等分。
编写一个函数,求一个给定的整型数组是否可以四等分,如果可以,返回一个布尔类型的true,如果不可以返回一个布尔类型的false。
限制条件:数组A最多有1,000,000项,数组中的整数取值范围介于 -1,000,000到1,000,000之间。
要求:函数的计算复杂度为O(N),使用的额外存储空间(除了输入数组外)最多为O(N)。
例子:
对于数组 A=[2,5,1,1,1,1,4,1,7,3,7] 存在下标2,7,9使得数组分成4个分片[2,5],[1,1,1,4],[7],[7],这三个分片内整数之和相等,所以对于这个数组,函数应该返回true
对于数组 A=[10,2,11,13,1,1,1,1,1],找不到能把数组四等分的下标,所以函数应该返回false。
- 这里时间复杂度没有达到O(N),日后有空再看看
public class Main { public static void main(String[] args) { int[] data = new int[]{1,5,6 ,8, 1,5,1,5 ,5, 8,3,1 ,6, 10,1,1 }; System.out.println(get(data)); } private static boolean get(int[] param){ //下标 int firstIndex = 0, lastIndex = param.length - 1; //数据 long firstData = 0L,lastData = 0L; firstData += param[firstIndex]; lastData += param[lastIndex]; while (firstIndex < lastIndex - 1){ if(firstData < lastData){ firstIndex ++; firstData += param[firstIndex]; }else if (firstData > lastData){ lastIndex -- ; lastData += param[lastIndex]; }else { int secondIndex = firstIndex + 2; int thirdIndex = lastIndex - 2; long secondData = 0L; long thirdData = 0L; while (secondIndex < thirdIndex-1 ){ if(firstData > secondData){ secondData += param[secondIndex]; //判断数据是否已经相等 if (firstData != secondData){ secondIndex ++ ; } } if (lastData > thirdData){ thirdData += param[thirdIndex]; //判断数据是否已经相等 if (lastData != thirdData){ thirdIndex -- ; } } if( firstData < secondData || lastData < thirdData){ //出现数据已经不正确直接 break; } if (firstData == secondData && secondData == thirdData){ // 下标相差2 if(secondIndex + 1 == thirdIndex - 1){ // 成功 System.out.println("第一个下标:" + (firstIndex + 1)); System.out.println("第二个下标:" + (secondIndex + 1)); System.out.println("第三个下标:" + (lastIndex - 1)); return true; }else { // 数值均相等,但不能分成4个数组,重新计算 break; } } } // 1 == 4 ,但无法分割数组,继续尝试 firstIndex ++; lastIndex --; firstData += param[firstIndex]; lastData += param[lastIndex]; } } return false; }}
阅读全文
1 0
- 平均分割数组
- js平均分割数组
- python list 平均 分割 等分
- 数组取平均压缩
- 数组分割
- 数组分割
- 数组分割
- 数组分割
- 数组分割
- 数组分割
- 数组分割
- 数组分割
- 数组分割
- 数组分割
- 数组分割
- 数组分割
- 数组分割
- 数组分割
- Servlet仿CSDN动态验证码的生成-带数字和字母
- 【C#】NPlot使用
- Electron入门资源
- android基本架构
- LotusScript解析XML字符串
- 平均分割数组
- unity学习之飞机大战制作 关键点(四) 设置敌机和奖励物品的生成和随机
- 类调用堆栈stdcall thiscall
- 表单标签
- C#控制台程序中处理2个关闭事件的代码实例
- C++调用Matlab && Matlab调用C++
- mysql中存在索引但不使用情况详解
- Java8的CompletableFuture异步调用
- enable_shared_from_this模板类使用完全解析