This post is also a good reference but it seems there is no MLE in C++ when using long long variable.

And here is my code based on yours and the C++ post.

public class Solution {
public int numberOfArithmeticSlices(int[] A) {
int res = 0;
List<HashMap<Integer, Integer>> l = new ArrayList<HashMap<Integer, Integer>>(A.length);
l.add(new HashMap<Integer, Integer>());
for (int i=1; i<A.length; ++i) {
HashMap<Integer, Integer> curMap = new HashMap<Integer, Integer>();
l.add(curMap);
for (int j=i-1; j>=0; --j) {
long dif = (long)A[i] - (long)A[j];
if (dif>Integer.MAX_VALUE || dif<Integer.MIN_VALUE)
continue;
HashMap<Integer, Integer> preMap = l.get(j);
int tmpCur = curMap.getOrDefault((int)dif, 0);
int tmpPre = preMap.getOrDefault((int)dif, 0);
res += tmpPre;
curMap.put((int)dif, tmpCur+tmpPre+1);
}
}
return res;
}
}