diff --git a/second/week_06/72/MyCircularDeque.java b/second/week_06/72/MyCircularDeque.java new file mode 100644 index 0000000000000000000000000000000000000000..02cb3e7126b44a9903f3725e2039b03125f8c170 --- /dev/null +++ b/second/week_06/72/MyCircularDeque.java @@ -0,0 +1,141 @@ +class MyCircularDeque { + private Node head, + tail; + private int capacity; + private int size; + /** Initialize your data structure here. Set the size of the deque to be k. */ + public MyCircularDeque(int k) { + capacity = k; + } + + /** Adds an item at the front of Deque. Return true if the operation is successful. */ + public boolean insertFront(int value) { + if (isFull()) + return false; + Node newNode = new Node(value); + if (isEmpty()) { + head = tail = newNode; + } else { + head.setPrev(newNode); + newNode.setNext(head); + head = newNode; + } + size++; + return true; + } + + /** Adds an item at the rear of Deque. Return true if the operation is successful. */ + public boolean insertLast(int value) { + if (isFull()) + return false; + Node newNode = new Node(value); + if (isEmpty()) { + head = tail = newNode; + } else { + tail.setNext(newNode); + newNode.setPrev(tail); + tail = newNode; + } + size++; + return true; + } + + /** Deletes an item from the front of Deque. Return true if the operation is successful. */ + public boolean deleteFront() { + if (isEmpty()) + return false; + head = head.getNext(); + if (head != null) + head.setPrev(null); + size--; + return true; + } + + /** Deletes an item from the rear of Deque. Return true if the operation is successful. */ + public boolean deleteLast() { + if (isEmpty()) + return false; + tail = tail.getPrev(); + if (tail != null) + tail.setNext(null); + size--; + return true; + } + + /** Get the front item from the deque. */ + public int getFront() { + if (isEmpty()) + return -1; + return head.getValue(); + } + + /** Get the last item from the deque. */ + public int getRear() { + if (isEmpty()) + return -1; + return tail.getValue(); + } + + /** Checks whether the circular deque is empty or not. */ + public boolean isEmpty() { + return size == 0; + } + + /** Checks whether the circular deque is full or not. */ + public boolean isFull() { + return size == capacity; + } + + class Node { + private int value; + private Node next; + private Node prev; + + public Node(int value) { + setValue(value); + } + + public Node(int value, Node next) { + setValue(value); + setNext(next); + } + + public void setValue(int value) { + this.value = value; + } + + public void setNext(Node next) { + this.next = next; + } + + public void setPrev(Node prev) { + this.prev = prev; + } + + public int getValue() { + return value; + } + + public Node getNext() { + return next; + } + + public Node getPrev() { + return prev; + } + } + +} + +/** + * Your MyCircularDeque object will be instantiated and called as such: + * MyCircularDeque obj = new MyCircularDeque(k); + * boolean param_1 = obj.insertFront(value); + * boolean param_2 = obj.insertLast(value); + * boolean param_3 = obj.deleteFront(); + * boolean param_4 = obj.deleteLast(); + * int param_5 = obj.getFront(); + * int param_6 = obj.getRear(); + * boolean param_7 = obj.isEmpty(); + * boolean param_8 = obj.isFull(); + */ \ No newline at end of file diff --git a/second/week_06/72/MyCircularQueue.java b/second/week_06/72/MyCircularQueue.java new file mode 100644 index 0000000000000000000000000000000000000000..e6e486217673803ed161b520883b6d762d090084 --- /dev/null +++ b/second/week_06/72/MyCircularQueue.java @@ -0,0 +1,71 @@ +class MyCircularQueue { + private int[] data; + private int headIndex; + private int tailIndex; + private int capacity; + + /** Initialize your data structure here. Set the size of the queue to be k. */ + public MyCircularQueue(int k) { + capacity = k; + data = new int[capacity]; + for (int i = 0; i < capacity; ++i) + data[i] = -1; + } + + /** Insert an element into the circular queue. Return true if the operation is successful. */ + public boolean enQueue(int value) { + if (isFull()) + return false; + data[tailIndex] = value; + tailIndex = getNextIndex(tailIndex); + return true; + } + + /** Delete an element from the circular queue. Return true if the operation is successful. */ + public boolean deQueue() { + if (isEmpty()) + return false; + data[headIndex] = -1; + headIndex = getNextIndex(headIndex); + return true; + } + + /** Get the front item from the queue. */ + public int Front() { + return data[headIndex]; + } + + /** Get the last item from the queue. */ + public int Rear() { + return data[getPrevIndex(tailIndex)]; + } + + /** Checks whether the circular queue is empty or not. */ + public boolean isEmpty() { + return data[headIndex] == -1; + } + + /** Checks whether the circular queue is full or not. */ + public boolean isFull() { + return data[tailIndex] == -1; + } + + private int getNextIndex(int currentIndex) { + return currentIndex + 1 == capacity ? 0: currentIndex + 1; + } + + private int getPrevIndex(int index) { + return index == 0 ? capacity - 1: index - 1; + } +} + +/** + * Your MyCircularQueue object will be instantiated and called as such: + * MyCircularQueue obj = new MyCircularQueue(k); + * boolean param_1 = obj.enQueue(value); + * boolean param_2 = obj.deQueue(); + * int param_3 = obj.Front(); + * int param_4 = obj.Rear(); + * boolean param_5 = obj.isEmpty(); + * boolean param_6 = obj.isFull(); + */ \ No newline at end of file diff --git a/second/week_06/72/MyHashMap.java b/second/week_06/72/MyHashMap.java new file mode 100644 index 0000000000000000000000000000000000000000..51e69b2d2f7e96ae9a7a4ed65a9d6e130d6e117d --- /dev/null +++ b/second/week_06/72/MyHashMap.java @@ -0,0 +1,113 @@ +class MyHashMap { + private int size; + private int capacity = 769; + private Node[] data; + + /** Initialize your data structure here. */ + public MyHashMap() { + data = new Node[capacity]; + } + + /** value will always be non-negative. */ + public void put(int key, int value) { + int index = hash(key, capacity); + if (data[index] == null) { + data[index] = new Node(key, value); + } else { + Node tailNode = data[index]; + while (tailNode != null) { + if (key == tailNode.getKey()) { + tailNode.setValue(value); + return; + } + if (tailNode.getNext() == null) { + tailNode.setNext(new Node(key, value)); + tailNode = null; + } else + tailNode = tailNode.getNext(); + } + } + size++; + } + + /** Returns the value to which the specified key is mapped, or -1 if this map contains no mapping for the key */ + public int get(int key) { + int index = hash(key, capacity); + Node node = data[index]; + while (node != null) { + if (key == node.getKey()) + return node.getValue(); + node = node.getNext(); + } + return -1; + } + + /** Removes the mapping of the specified value key if this map contains a mapping for the key */ + public void remove(int key) { + int index = hash(key, capacity); + Node prev = null; + Node node = data[index]; + while (node != null) { + if (key == node.getKey()) { + if (prev == null) + data[index] = node.getNext(); + else + prev.next = node.getNext(); + size--; + return; + } + prev = node; + node = node.getNext(); + } + } + + private int hash(int key, int cap) { + return key % (cap - 1); + } + + + class Node { + private int key; + private int value; + private Node next; + + public Node(int key, int value) { + setKey(key); + setValue(value); + } + + public void setKey(int key) { + this.key = key; + } + + public void setValue(int value) { + this.value = value; + } + + public void setNext(Node next) { + this.next = next; + } + + public int getKey() { + return key; + } + + public int getValue() { + return value; + } + + public Node getNext() { + return next; + } + } + +} + + +/** + * Your MyHashMap object will be instantiated and called as such: + * MyHashMap obj = new MyHashMap(); + * obj.put(key,value); + * int param_2 = obj.get(key); + * obj.remove(key); + */ \ No newline at end of file diff --git a/second/week_06/72/MyHashSet.java b/second/week_06/72/MyHashSet.java new file mode 100644 index 0000000000000000000000000000000000000000..fa093c4d739f916f9d4ed796df254e1ad5293286 --- /dev/null +++ b/second/week_06/72/MyHashSet.java @@ -0,0 +1,94 @@ +class MyHashSet { + private int size; + private int capacity = 769; + private Node[] data; + /** Initialize your data structure here. */ + public MyHashSet() { + data = new Node[capacity]; + } + + public void add(int key) { + int index = hashIndex(key, capacity); + Node current = data[index]; + if (current == null) { + data[index] = new Node(key); + size++; + } else { + while (current.getValue() != key && current.getNext() != null) + current = current.next; + if (current.getValue() != key) { + current.setNext(new Node(key)); + size++; + } + } + } + + public void remove(int key) { + int index = hashIndex(key, capacity); + Node current = data[index], + prev = null; + while (current != null) { + if (current.getValue() == key) { + if (null != prev) + prev.setNext(current.getNext()); + else + data[index] = current.getNext(); + size--; + return; + } + prev = current; + current = current.getNext(); + } + } + + /** Returns true if this set contains the specified element */ + public boolean contains(int key) { + int index = hashIndex(key,capacity); + Node current = data[index]; + while (current != null) { + if (current.getValue() == key) + return true; + current = current.getNext(); + } + return false; + } + + private int hashIndex(int key, int cap) { + return key % (cap - 1); + } + + class Node { + private int value; + private Node next; + + public Node(int value) { + setValue(value); + } + + public void setNext(Node next) { + this.next = next; + } + + public void setValue(int value) { + this.value = value; + } + + public Node getNext() { + return next; + } + + public int getValue() { + return value; + } + } + + +} + +/** + * Your MyHashSet object will be instantiated and called as such: + * MyHashSet obj = new MyHashSet(); + * obj.add(key); + * obj.remove(key); + * boolean param_3 = obj.contains(key); + */ \ No newline at end of file diff --git a/second/week_06/72/MyLinkedList.java b/second/week_06/72/MyLinkedList.java new file mode 100644 index 0000000000000000000000000000000000000000..8a5cb15c6439bc5ac9e09ff5069e2a712df1af63 --- /dev/null +++ b/second/week_06/72/MyLinkedList.java @@ -0,0 +1,120 @@ +class MyLinkedList { + private int size; + private Node head; + private Node tail; + + /** Initialize your data structure here. */ + public MyLinkedList() { + + } + + /** Get the value of the index-th node in the linked list. If the index is invalid, return -1. */ + public int get(int index) { + if (size <= index) + return -1; + Node current = head; + for (int i = 0; i < index; ++i) + current = current.getNext(); + return current.getValue(); + } + + /** Add a node of value val before the first element of the linked list. After the insertion, the new node will be the first node of the linked list. */ + public void addAtHead(int val) { + Node newNode = new Node(val); + if (size == 0) { + head = tail = newNode; + } else { + newNode.setNext(head); + head = newNode; + } + size++; + } + + /** Append a node of value val to the last element of the linked list. */ + public void addAtTail(int val) { + Node newNode = new Node(val); + if (size == 0) { + head = tail = newNode; + } else { + tail.setNext(newNode); + tail = newNode; + } + size++; + } + + /** Add a node of value val before the index-th node in the linked list. If index equals to the length of linked list, the node will be appended to the end of linked list. If index is greater than the length, the node will not be inserted. */ + public void addAtIndex(int index, int val) { + if (index > size) + return; + Node newNode = new Node(val); + if (index == 0) { + addAtHead(val); + return; + } + Node current = head; + for (int i = 1; i < index; ++i) + current = current.getNext(); + newNode.setNext(current.getNext()); + current.setNext(newNode); + if (index == size) + tail = newNode; + ++size; + } + + /** Delete the index-th node in the linked list, if the index is valid. */ + public void deleteAtIndex(int index) { + if (index >= size) + return; + if (size == 1) { + head = tail = null; + } else if (index == 0) { + head = head.getNext(); + } else { + Node current = head; + for (int i = 0; i < index - 1; ++i) + current = current.getNext(); + current.setNext(current.getNext().getNext()); + if (index == 0) + head = head.getNext(); + else if (index == size - 1) + tail = current; + } + --size; + } + + class Node { + private int value; + private Node next; + + public Node(int value) { + setValue(value); + } + + public void setValue(int value) { + this.value = value; + } + + public void setNext(Node next) { + this.next = next; + } + + public int getValue() { + return value; + } + + public Node getNext() { + return next; + } + } + +} + +/** + * Your MyLinkedList object will be instantiated and called as such: + * MyLinkedList obj = new MyLinkedList(); + * int param_1 = obj.get(index); + * obj.addAtHead(val); + * obj.addAtTail(val); + * obj.addAtIndex(index,val); + * obj.deleteAtIndex(index); + */ \ No newline at end of file