Arithmetic Subarrays

给一个数组nums,和一个query数组l和r, 求query中的nums是不是等差数列

这个…就是sort了判断

class Solution {
    public List<Boolean> checkArithmeticSubarrays(int[] nums, int[] l, int[] r) {
        List<Boolean> res = new ArrayList<>();
        int[] pre = new int[nums.length + 1];
        for(int i = 1; i < nums.length + 1; i++) {
            pre[i] = pre[i-1] + nums[i - 1];
        }
        for(int i = 0; i < l.length; i++) {
            boolean t = test(nums, l[i], r[i], pre[r[i]+1] - pre[l[i]]);
            res.add(t);
        }
        return res;
    } 
    private boolean test(int[] nums, int l, int r, int sum) {
        int[] aux = new int[r - l + 1];
        int k = 0;
        for(int i = l; i <= r; i++) {
            aux[k++] = nums[i];
        }
        Arrays.sort(aux); 
        int d = aux[1] - aux[0];
        for(int i = 0; i < r - l; i++) {
            if(aux[i + 1] - aux[i] != d)
                return false;
        }
        return true;
    }
}