Design HashSet

Design 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)
 1 class MyHashSet {
 2     final ListNode[] nodes = new ListNode[10000];
 3     /** Initialize your data structure here. */
 4     public MyHashSet() {
 5
 6     }
 7
 8     public void add(int key) {
 9         int i = idx(key);
10         ListNode first = nodes[i];
11         if (first == null) {
12             nodes[i] = new ListNode(key);
13         } else if (!exists(first, key)) {
14             ListNode newNode = new ListNode(key);
15             newNode.next = nodes[i];
16             nodes[i] = newNode;
17         }
18     }
19
20     public void remove(int key) {
21         int i = idx(key);
22         if (nodes[i] == null) {
23             return;
24         }
25         ListNode current = nodes[i];
26         ListNode previous = null;
27         while (current != null) {
28             if (current.key == key) {
29                 if (previous != null) {
30                     previous.next = current.next;
31                 } else {
32                     nodes[i] = current.next;
33                 }
34                 break;
35             } else {
36                 previous = current;
37                 current = current.next;
38             }
39         }
40     }
41
42     /** Returns true if this set contains the specified element */
43     public boolean contains(int key) {
44         int i = idx(key);
45         ListNode first = nodes[i];
46         if (first == null) {
47             return false;
48         }
49         return exists(first, key);
50     }
51
52     int idx(int key) {
53         return key % nodes.length;
54     }
55
56     private boolean exists(ListNode node, int key) {
57         ListNode current = node;
58         while (current != null) {
59             if (current.key == key) {
60                 return true;
61             }
62             current = current.next;
63         }
64         return false;
65     }
66 }
67
68
69 class ListNode {
70     int key;
71     ListNode next;
72
73     ListNode(int key) {
74         this.key = key;
75     }
76 }

原文地址:https://www.cnblogs.com/beiyeqingteng/p/11334086.html

时间: 2024-07-31 22:23:04

Design HashSet的相关文章

705.Design HashSet

Design 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

[LeetCode] Design HashSet 设计HashSet

Design 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

[LeetCode&Python] Problem 705. Design HashSet

Design 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

[Swift]LeetCode705. 设计哈希集合 | Design HashSet

Design 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

LeetCode 705. Design HashSet (设计哈希集合)

题目标签:HashMap 题目让我们设计一个 hashset,有add,contains,remove 功能. 建立一个boolean array,index 是数字的值,具体看code. Java Solution: Runtime: 58 ms, faster than 90.21% Memory Usage: 56.3 MB, less than 68.53% 完成日期:03/18/2019 关键点:boolean array class MyHashSet { boolean [] se

【LeetCode】设计题 design(共38题)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [146]LRU Cache [155]Min Stack [170]Two Sum III - Data structure design [173]Binary Search Tree Iterator [208]Implement Trie (Prefix Tree) [211]Add and Search Word - Data structure desig

【LeetCode】哈希表 hash_table(共88题)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [1]Two Sum (2018年11月9日,k-sum专题,算法群衍生题) 给了一个数组 nums, 和一个 target 数字,要求返回一个下标的 pair, 使得这两个元素相加等于 target . 题解:我这次最大范围的优化代码, hash-table + one pass,时间复杂度 O(N),空间复杂度 O(N).重点在于动态找,一边生成hash-tabl

170. Two Sum III - Data structure design

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 numbers which sum is equal to the value. For example, add(1); ad

【leetcode】355. Design Twitter

题目描述: Design a simplified version of Twitter where users can post tweets, follow/unfollow another user and is able to see the 10 most recent tweets in the user's news feed. Your design should support the following methods: postTweet(userId, tweetId):