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.
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
摘要:題目鏈接題目分析設計一個哈希類。需要有添加元素函數,判斷元素存在的函數,移除元素函數。思路這真的沒什么好說的了我把要存的值作為數組的鍵存儲。最終代碼若覺得本文章對你有用,歡迎用愛發電資助。 D87 705. Design HashSet 題目鏈接 705. Design HashSet 題目分析 設計一個哈希類。 需要有add添加元素函數,contains判斷元素存在的函數,remov...
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...
摘要:題目鏈接和基本一樣,都可以用,但是大了之后會有很多無效的時間保存在里面,要的話,可能需要遍歷或者用輔助,時間復雜度超過,所以用一個來做,每次根據新的改變。 359. Logger Rate Limiter 題目鏈接:https://leetcode.com/problems... 和Design Hit Counter基本一樣,都可以用hashmap,但是timestamp大了之后會有...
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...
摘要:題目鏈接直接用一個,結果了看了加了個,不過感覺沒什么必要加,反正保存的都一樣,只是的時間大于,用可以保證。看了題目條件是可以隨便返回一個值,但是不讓這么做。很無語啊如果這道題要求要求的是的,那就和一樣了。 Design Phone Directory 題目鏈接:https://leetcode.com/problems... 直接用一個set,結果tle了= = public clas...
閱讀 2742·2021-11-24 10:23
閱讀 1153·2021-11-17 09:33
閱讀 2503·2021-09-28 09:41
閱讀 1409·2021-09-22 15:55
閱讀 3641·2019-08-29 16:32
閱讀 1903·2019-08-29 16:25
閱讀 1056·2019-08-29 11:06
閱讀 3421·2019-08-29 10:55