twoSum原题的测试集是一种输入只有一个答案(题干告诉的条件),这里这个要考虑重复。
```Java
public long countPairs(ArrayList<Integer> A, int n, int sum) {
long count = 0;
HashMap<Integer, Integer> hashMap = new HashMap<>();
for (int i = 0; i < n; i++) {
if (hashMap.containsKey(sum - A.get(i))) {
count += hashMap.get(sum - A.get(i));
}
if (hashMap.containsKey(A.get(i))) {
hashMap.put(A.get(i), hashMap.get(A.get(i)) + 1);
} else {
hashMap.put(A.get(i), 1);
}
}
return count;
}
```