Hash Tables
4-SUM. Given an array a[] of nn integers, the 4-SUM problem is to determine if there exist distinct indices i, j, k, and l such that a[i]+a[j]=a[k]+a[l]. Design an algorithm for the 4-SUM problem that takes time proportional to n 2 n^2 n2 (under suitable technical assumptions).
遍历i、j,先查找hash表中是否已存在a[i]+a[j],没有则将和存入hash表中。
public class FourSUM {
public boolean exist(int[] a) {
Set<Integer> hash = new HashSet<>();
for (int i = 0; i < a.length - 1; i++) {
for (int j = i + 1; j < a.length; j++) {
if (hash.contains(a[i] + a[j])) {
return true;
}
hash.add(a[i] + a[j]);
}
}
return false;
}
}
Hashing with wrong hashCode() or equals(). Suppose that you implement a data type ?????????????? for use in a ????.????.???????.
- Describe what happens if you override ????????() but not ??????().
- Describe what happens if you override ??????() but not ????????().
- Describe what happens if you override ????????() but implement public boolean equals(OlympicAthlete that) instead of public boolean equals(Object that).
-
只重写了hashCode(),但equals()仍是只当两引用为同一对象时才返回true。那么当插入两个有相同name的对象时,hashCode()相同,但equals()为false,会在同一下标处插入两个相同的name。
-
只重写了equals(),但hashCode()仍是默认返回对象的地址。那么当插入两个有相同name 的对象时,equals()为true,但hashCode()不同,会在不同下标处插入两个相同的name。
-
用以下用例进行测试,证明调用仍是equals(Object that)。分析原因,可能是因为java的泛型是通过类型擦除实现的,在运行时所有的Key类型都会被擦除为Object,因此也只会调用equals(Object that)。
import java.util.HashMap; public class Test { String name; public Test(String name) { this.name = name; } @Override public int hashCode() { return name.hashCode(); } public boolean equals(Test that) { if (that == null) { return false; } return that.name.equals(this.name); } public static void main(String[] args) { HashMap<Test, Boolean> map = new HashMap<>(); map.put(new Test("Li"), true); map.put(new Test("Li"), true); map.put(new Test("Li"), true); map.put(new Test("Li"), true); System.out.println(map.size()); // 输出 4 } }