Java差分数组
2022/3/21 17:57:50
本文主要是介绍Java差分数组,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
package daweiguo.other; import java.util.Arrays; /** * @Author DaWeiGuo * @Date 2022/3/21 16:16 * @Desc: 差分数组 */ public class DifferenceArray { public static void main(String[] args) { int[] originalArray = new int[]{1,3,7,5,2}; DifferenceArray differenceArray = new DifferenceArray(originalArray.length); differenceArray.add(2,4,5); differenceArray.add(1,3,2); differenceArray.add(0,2,-3); differenceArray.reflectToOriginalArray(originalArray); System.out.println(Arrays.toString(originalArray)); } int[] differArray; public DifferenceArray(int len){ differArray = new int[len]; initDifferenceArray(); } public boolean add(int l,int r,int value){ if(l>= differArray.length) return false; differArray[l] += value; if(r+1<differArray.length) differArray[r+1] -= value; return true; } public void initDifferenceArray(){ Arrays.fill(differArray,0); } public void resultArray(){ for(int i=1;i<differArray.length;i++){ differArray[i] +=differArray[i-1]; } } public boolean reflectToOriginalArray(int[] originalArray){ if(originalArray.length!=differArray.length) return false; resultArray(); for(int i=0;i<originalArray.length;i++){ originalArray[i] += differArray[i]; } return true; } }
这篇关于Java差分数组的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-26手写消息中间件:从零开始的指南
- 2024-11-26Java语音识别项目资料:新手入门教程
- 2024-11-26JAVA语音识别项目资料:新手入门教程
- 2024-11-26Java语音识别项目资料:入门与实践指南
- 2024-11-26Java云原生资料入门教程
- 2024-11-26Java云原生资料入门教程
- 2024-11-26Java云原生资料:新手入门教程
- 2024-11-25Java创意资料:新手入门的创意学习指南
- 2024-11-25JAVA对接阿里云智能语音服务资料详解:新手入门指南
- 2024-11-25Java对接阿里云智能语音服务资料详解