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