forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_1630.java
35 lines (32 loc) · 1.06 KB
/
_1630.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package com.fishercoder.solutions;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class _1630 {
public static class Solution1 {
public List<Boolean> checkArithmeticSubarrays(int[] nums, int[] l, int[] r) {
List<Boolean> result = new ArrayList<>();
for (int i = 0; i < l.length; i++) {
if (isArithmetic(nums, l[i], r[i])) {
result.add(true);
} else {
result.add(false);
}
}
return result;
}
private boolean isArithmetic(int[] nums, int start, int end) {
List<Integer> list = new ArrayList<>();
for (int i = start; i <= end; i++) {
list.add(nums[i]);
}
Collections.sort(list);
for (int i = 1; i < list.size(); i++) {
if (list.get(i) - list.get(i - 1) != list.get(1) - list.get(0)) {
return false;
}
}
return true;
}
}
}