资讯专栏INFORMATION COLUMN

[LeetCode] 705. Design HashSet

snowell / 2496人阅读

Problem

esign a HashSet without using any built-in hash table libraries.

To be specific, your design should include these functions:

add(value): Insert a value into the HashSet.
contains(value) : Return whether the value exists in the HashSet or not.
remove(value): Remove a value in the HashSet. If the value does not exist in the HashSet, do nothing.

Example:

MyHashSet hashSet = new MyHashSet();
hashSet.add(1);
hashSet.add(2);
hashSet.contains(1); // returns true
hashSet.contains(3); // returns false (not found)
hashSet.add(2);
hashSet.contains(2); // returns true
hashSet.remove(2);
hashSet.contains(2); // returns false (already removed)

Note:

All values will be in the range of [0, 1000000].
The number of operations will be in the range of [1, 10000].
Please do not use the built-in HashSet library.

Solution
class MyHashSet {
    
    class Bucket {
        ListNode head = new ListNode(-1);
    }
    
    class ListNode {
        int key;
        ListNode next;
        ListNode(int key) {
            this.key = key;
        }
    }
    
    int size = 10000;
    Bucket[] buckets = new Bucket[size];
    
    int hash(int key) {
        return key%size;
    }
    
    ListNode find(Bucket bucket, int key) {
        ListNode head = bucket.head;
        ListNode pre = head;
        while (head != null && head.key != key) {
            pre = head;
            head = head.next;
        }
        return pre;
    }
    
    public void add(int key) {
        int i = hash(key);
        if (buckets[i] == null) buckets[i] = new Bucket();
        ListNode pre = find(buckets[i], key);
        if (pre.next == null) pre.next = new ListNode(key);
    }
    
    public void remove(int key) {
        int i = hash(key);
        if (buckets[i] != null) {
            ListNode pre = find(buckets[i], key);
            if (pre.next != null) pre.next = pre.next.next;
        }
    }
    
    /** Returns true if this set contains the specified element */
    public boolean contains(int key) {
        int i = hash(key);
        if (buckets[i] != null) {
            ListNode pre = find(buckets[i], key);
            return pre.next == null ? false : true;
        }
        return false;
    }
}

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/77181.html

相关文章

  • Leetcode PHP题解--D87 705. Design HashSet

    摘要:题目链接题目分析设计一个哈希类。需要有添加元素函数,判断元素存在的函数,移除元素函数。思路这真的没什么好说的了我把要存的值作为数组的键存储。最终代码若觉得本文章对你有用,欢迎用爱发电资助。 D87 705. Design HashSet 题目链接 705. Design HashSet 题目分析 设计一个哈希类。 需要有add添加元素函数,contains判断元素存在的函数,remov...

    why_rookie 评论0 收藏0
  • [LeetCode] Design Phone Directory

    Problem Design a Phone Directory which supports the following operations: get: Provide a number which is not assigned to anyone.check: Check if a number is available or not.release: Recycle or release...

    wangbinke 评论0 收藏0
  • 359. Logger Rate Limiter & 362. Design Hit Cou

    摘要:题目链接和基本一样,都可以用,但是大了之后会有很多无效的时间保存在里面,要的话,可能需要遍历或者用辅助,时间复杂度超过,所以用一个来做,每次根据新的改变。 359. Logger Rate Limiter 题目链接:https://leetcode.com/problems... 和Design Hit Counter基本一样,都可以用hashmap,但是timestamp大了之后会有...

    go4it 评论0 收藏0
  • [LeetCode] 170. Two Sum III - Data structure desig

    Problem Design and implement a TwoSum class. It should support the following operations: add and find. add - Add the number to an internal data structure.find - Find if there exists any pair of number...

    dack 评论0 收藏0
  • Design Phone Directory

    摘要:题目链接直接用一个,结果了看了加了个,不过感觉没什么必要加,反正保存的都一样,只是的时间大于,用可以保证。看了题目条件是可以随便返回一个值,但是不让这么做。很无语啊如果这道题要求要求的是的,那就和一样了。 Design Phone Directory 题目链接:https://leetcode.com/problems... 直接用一个set,结果tle了= = public clas...

    NicolasHe 评论0 收藏0

发表评论

0条评论

snowell

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<