国产xxxx99真实实拍_久久不雅视频_高清韩国a级特黄毛片_嗯老师别我我受不了了小说

資訊專欄INFORMATION COLUMN

[LeetCode] 705. Design HashSet

snowell / 2523人閱讀

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;
    }
}

文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。

轉載請注明本文地址:http://specialneedsforspecialkids.com/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元查看
<