/**
* 自己的代码,将每一个子数组排序然后判断是否为等差,是则res.add(true),不是则res.add(false)
* Runtime: 14 ms, faster than 90.75%
* Memory Usage: 39.6 MB, less than 40.60%
*/
class Solution {
public List<Boolean> checkArithmeticSubarrays(int[] nums, int[] l, int[] r) {
List<Boolean> res = new ArrayList<>();
for (int i = 0; i < l.length; i++) {
if (r[i] <= l[i]) {
res.add(false);
} else {
int[] curr = new int[r[i] - l[i] + 1];
for (int j = l[i], idx = 0; j <= r[i]; j++, idx++) { // 创建数组的过程可以用int[] curr = Arrays.copyOfRange(nums, l[i], r[i] + 1);代替
curr[idx] = nums[j];
}
Arrays.sort(curr);
int delta = curr[1] - curr[0];
boolean currBool = true;
for (int k = 2; k < curr.length; k++) {
if (curr[k] - curr[k - 1] != delta) {
currBool = false;
break;
}
}
res.add(currBool);
}
}
return res;
}
}