forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_446.java
35 lines (29 loc) · 1.03 KB
/
_446.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.HashMap;
import java.util.Map;
public class _446 {
public static class Solution1 {
/**
* reference: https://discuss.leetcode.com/topic/67413/detailed-explanation-for-java-o-n-2-solution
*/
public int numberOfArithmeticSlices(int[] A) {
int res = 0;
Map<Integer, Integer>[] map = new Map[A.length];
for (int i = 0; i < A.length; i++) {
map[i] = new HashMap<>(i);
for (int j = 0; j < i; j++) {
long diff = (long) A[i] - A[j];
if (diff <= Integer.MIN_VALUE || diff > Integer.MAX_VALUE) {
continue;
}
int d = (int) diff;
int c1 = map[i].getOrDefault(d, 0);
int c2 = map[j].getOrDefault(d, 0);
res += c2;
map[i].put(d, c1 + c2 + 1);
}
}
return res;
}
}
}