-
Notifications
You must be signed in to change notification settings - Fork 0
/
ArithmeticSlicesII.java
29 lines (26 loc) · 988 Bytes
/
ArithmeticSlicesII.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
import java.util.HashMap;
import java.util.Map;
public class ArithmeticSlicesII {
static class Solution {
public int numberOfArithmeticSlices(int[] nums) {
Map<Integer, HashMap> m = new HashMap<Integer, HashMap>();
int res = 0;
for (int i=0; i<nums.length; i++) {
m.put(i, new HashMap<Long, Integer>());
for (int j = 0; j < i; j++) {
long absDiff = (long) nums[i] - (long) nums[j];
int prev = (int) (m.get(j).getOrDefault(absDiff, 0));
int cur = (int) (m.get(i).getOrDefault(absDiff, 0));
m.get(i).put(absDiff, cur+prev+1);
res += (int) prev;
}
}
return res;
}
}
public static void main(String[] args) {
Solution s = new Solution();
int res = s.numberOfArithmeticSlices(new int[]{2,6,4,10});
System.out.println(res);
}
}