Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 48 additions & 5 deletions Sample.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,50 @@
// Time Complexity :
// Space Complexity :
// Did this code successfully run on Leetcode :
// Any problem you faced while coding this :
class MyHashSet {

private int buckets = 1000;
private int bucketItems = 1001;
private boolean[][] storage;

// Your code here along with comments explaining your approach
public MyHashSet() {
storage = new boolean[buckets][];
}

private int hash1(int key) {
return key % buckets;
}

private int hash2(int key) {
return key / buckets;
}

public void add(int key) {
int bucket = hash1(key);
int bucketItem = hash2(key);

if (storage[bucket] == null) {
if (bucket == 0) {
storage[bucket] = new boolean[bucketItems + 1];
} else {
storage[bucket] = new boolean[bucketItems];
}
}

storage[bucket][bucketItem] = true;
}

public void remove(int key) {
int bucket = hash1(key);
int bucketItem = hash2(key);

if (storage[bucket] != null) {
storage[bucket][bucketItem] = false;
}
}

public boolean contains(int key) {
int bucket = hash1(key);
int bucketItem = hash2(key);

return storage[bucket] != null &&
storage[bucket][bucketItem];
}
}