diff --git a/Sample.java b/Sample.java index 1739a9cb..98961353 100644 --- a/Sample.java +++ b/Sample.java @@ -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]; + } +}