标准型布隆过滤器

http://www.lintcode.com/zh-cn/problem/standard-bloom-filter/

import java.util.TreeSet;

public class StandardBloomFilter {
  private Map map = new HashMap<>();

    /*
    * @param k: An integer
    */
    public StandardBloomFilter(int k) {
        // do intialization if necessary
    }

    /*
     * @param word: A string
     * @return: nothing
     */
    public void add(String word) {
        // write your code here
        Integer integer = map.get(word);
        if (integer == null) {
            integer = 1;
        } else {
            integer++;
        }
        map.put(word, integer);
    }

    /*
     * @param word: A string
     * @return: nothing
     */
    public void remove(String word) {
        // write your code here
        Integer integer = map.get(word);
        if (integer != null) {
            integer--;
            if (integer == 0) {
                map.remove(word);
            } else {
                map.put(word, integer);
            }
        }
    }

    /*
     * @param word: A string
     * @return: True if contains word
     */
    public boolean contains(String word) {
        // write your code here
        return map.containsKey(word);
    }
}

你可能感兴趣的:(标准型布隆过滤器)