刷题时遇到的,网上查了下资料,整理了下这两道题,直接上代码
public class StandardBloomFilter {
private static final int BIT_SIZE = 2 << 28;//二进制向量的位数,相当于能存储1000万条url左右,误报率为千万分之一
private BitSet bits = new BitSet(BIT_SIZE);
private Hash[] func;//用于存储随机哈希值对象
/*
* @param k: An integer
*/public StandardBloomFilter(int k) {
// do intialization if necessary
func = new Hash[k];
for(int i=0;i<k;i++){
func[i] = new Hash(BIT_SIZE,i+1);
}
}
/*
* @param word: A string
* @return: nothing
*/
public void add(String word) {
// write your code here
if(word!=null){
for(Hash f : func){
bits.set(f.hash(word),true);
}
}
}
/*
* @param word: A string
* @return: True if contains word
*/
public boolean contains(String word) {
if(word==null){
return false;
}
boolean ret = true;
for(Hash f : func){
ret=ret&&bits.get(f.hash(word));
}
return ret;
}
public static class Hash{
private int size;//二进制向量数组大小
private int seed;//随机数种子
public Hash(int cap, int seed){
this.size = cap;
this.seed = seed;
}
/**
* 计算哈希值(也可以选用别的恰当的哈希函数)
*/
public int hash(String value){
int result = 0;
int len = value.length();
for(int i = 0; i < len; i++){
result = seed * result + value.charAt(i);
}
return (size - 1) & result;
}
}
}
------------------------------------------------------------------------------------------------------------------------------------------------
public class CountingBloomFilter {
private static final int BIT_SIZE = 100000;
private int[] bits;
private Hash[] func;
/*
* @param k: An integer
*/public CountingBloomFilter(int k) {
// do intialization if necessary
func = new Hash[k];
for(int i=0;i<k;i++){
func[i] = new Hash(BIT_SIZE, 2*i+3);
}
bits = new int[BIT_SIZE];
}
/*
* @param word: A string
* @return: nothing
*/
public void add(String word) {
// write your code here
if(word==null){
return;
}
for(Hash f : func){
bits[f.hash(word)]++;
}
}
/*
* @param word: A string
* @return: nothing
*/
public void remove(String word) {
// write your code here
for(Hash f : func){
int pos = f.hash(word);
if(bits[pos]>0){
bits[pos]--;
}
}
}
/*
* @param word: A string
* @return: True if contains word
*/
public boolean contains(String word) {
for(Hash f : func){
if(bits[f.hash(word)]==0){
return false;
}
}
return true;
}
public static class Hash{
private int size;//二进制向量数组大小
private int seed;//随机数种子
public Hash(int cap, int seed){
this.size = cap;
this.seed = seed;
}
/**
* 计算哈希值(也可以选用别的恰当的哈希函数)
*/
public int hash(String value){
int result = 0;
int len = value.length();
for(int i = 0; i < len; i++){
result = seed * result + value.charAt(i);
result%=size;
}
return result;
}
}
}
没什么难点,就不多讲了,主要是hash函数的实现和随机种子数的选择