diff --git "a/week_06/51/LettCode\351\242\230\350\247\243_51.md" "b/week_06/51/LettCode\351\242\230\350\247\243_51.md" new file mode 100644 index 0000000000000000000000000000000000000000..ff60ac07ed91e427b7ec2c065072ea01125b0f69 --- /dev/null +++ "b/week_06/51/LettCode\351\242\230\350\247\243_51.md" @@ -0,0 +1,571 @@ +[1188.限阻塞队列] + +```java +class BoundedBlockingQueue { + + + + private final Integer[] elements; + + + + private ReentrantLock lock; + + + + private int count; + + + + private Condition empty; + + + + private Condition full; + + + + private int putIndex; + + + + private int takeIndex; + + + + public BoundedBlockingQueue(int capacity) { + +​ if (capacity <= 0) + +​ throw new IllegalArgumentException(); + +​ this.elements = new Integer[capacity]; + +​ this.lock = new ReentrantLock(false); + +​ this.empty = lock.newCondition(); + +​ this.full = lock.newCondition(); + + } + + + + public void enqueue(int element) throws InterruptedException { + +​ final ReentrantLock lock = this.lock; + +​ lock.lockInterruptibly(); + +​ try{ + +​ while (count == elements.length) + +​ full.await(); + +​ elements[putIndex] = element; + +​ if (putIndex == elements.length - 1){ + +​ putIndex = 0; + +​ }else { + +​ putIndex++; + +​ } + +​ count++; + +​ empty.signal(); + +​ }finally { + +​ lock.unlock(); + +​ } + + } + + + + public int dequeue() throws InterruptedException { + +​ final ReentrantLock lock = this.lock; + +​ lock.lockInterruptibly(); + +​ try { + +​ while (count == 0) + +​ empty.await(); + +​ int result = elements[takeIndex]; + +​ elements[takeIndex] = null; + +​ if (takeIndex == elements.length - 1){ + +​ takeIndex = 0; + +​ }else { + +​ takeIndex++; + +​ } + +​ count--; + +​ full.signal(); + +​ return result; + +​ }finally { + +​ lock.unlock(); + +​ } + + } + + + + public int size() { + +​ final ReentrantLock lock = this.lock; + +​ lock.lock(); + +​ try { + +​ return count; + +​ } finally { + +​ lock.unlock(); + +​ } + + } + +} +``` + + + +#### [706. 设计哈希映射] + +```java +class MyHashMap { + + + + private final int[] arr; + + /** Initialize your data structure here. */ + + public MyHashMap() { + +​ arr = new int[1000001]; + +​ Arrays.fill(arr,-1); + + } + + + + /** value will always be non-negative. */ + + public void put(int key, int value) { + +​ arr[key] = value; + + } + + + + /** 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) { + +​ return arr[key]; + + } + + + + /** Removes the mapping of the specified value key if this map contains a mapping for the key */ + + public void remove(int key) { + +​ arr[key] = -1; + + } + +} +``` + +#### [622. 设计循环队列] + +```java +class MyCircularQueue { + + private final int[] elements; + + private int count; + + private int putIndex; + + private int takeIndex; + + // private ReentrantLock lock; + + /** Initialize your data structure here. Set the size of the queue to be k. */ + public MyCircularQueue(int k) { + if (k < 0) + throw new IllegalStateException(); + this.elements = new int[k]; + // this.lock = new ReentrantLock(false); + } + + /** Insert an element into the circular queue. Return true if the operation is successful. */ + public boolean enQueue(int value) { + // final ReentrantLock lock = this.lock; + // lock.lock(); + // try { + if (count == elements.length){ + return false; + }else { + elements[putIndex] = value; + if (putIndex == elements.length - 1){ + putIndex = 0; + }else { + putIndex++; + } + count++; + return true; + } + // }finally { + // lock.unlock(); + // } + } + + /** Delete an element from the circular queue. Return true if the operation is successful. */ + public boolean deQueue() { + // final ReentrantLock lock = this.lock; + // lock.lock(); + // try { + if (count == 0){ + return false; + }else { + elements[takeIndex] = -1; + if (takeIndex == elements.length - 1){ + takeIndex = 0; + }else { + takeIndex++; + } + count--; + return true; + } + // }finally { + // lock.unlock(); + // } + } + + /** Get the front item from the queue. */ + public int Front() { + // final ReentrantLock lock = this.lock; + // lock.lock(); + // try { + if (count == 0){ + return -1; + }else { + return elements[takeIndex]; + } + // }finally { + // lock.unlock(); + // } + } + + /** Get the last item from the queue. */ + public int Rear() { + // final ReentrantLock lock = this.lock; + // lock.lock(); + // try { + if (count == 0){ + return -1; + }else { + int result; + if (putIndex == 0){ + result = elements[elements.length - 1]; + } else { + result = elements[putIndex - 1]; + } + return result; + } + // }finally { + // lock.unlock(); + // } + } + + /** Checks whether the circular queue is empty or not. */ + public boolean isEmpty() { + // final ReentrantLock lock = this.lock; + // lock.lock(); + // try { + return count == 0?true:false; + // }finally { + // lock.unlock(); + // } + } + + /** Checks whether the circular queue is full or not. */ + public boolean isFull() { + // final ReentrantLock lock = this.lock; + // lock.lock(); + // try { + return count == elements.length?true:false; + // }finally { + // lock.unlock(); + // } + } +} +``` + +#### [707. 设计链表](https://leetcode-cn.com/problems/design-linked-list/) + +```java +class MyLinkedList { + + private static class Node { + int item; + Node next; + Node prev; + + Node(Node prev, int element, Node next) { + this.item = element; + this.next = next; + this.prev = prev; + } + } + private Node first; + + private Node last; + + private int size = 0; + + Node node(int index) { + if (index < (size >> 1)) { + Node x = first; + for (int i = 0; i < index; i++) + x = x.next; + return x; + } else { + Node x = last; + for (int i = size - 1; i > index; i--) + x = x.prev; + return x; + } + } + + /** 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 (index <0 || index >= size) + return -1; + return node(index).item; + } + + /** 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) { + final Node f = first; + final Node newNode = new Node(null, val, f); + first = newNode; + if (f == null) + last = newNode; + else + f.prev = newNode; + size++; + } + + /** Append a node of value val to the last element of the linked list. */ + public void addAtTail(int val) { + final Node l = last; + final Node newNode = new Node(l, val, null); + last = newNode; + if (l == null) + first = newNode; + else + l.next = 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 <0){ + addAtHead(val); + }else if (index < size){ + final Node node = node(index); + final Node pre = node.prev; + final Node newNode = new Node(pre, val, node); + node.prev = newNode; + if (pre == null){ + first = newNode; + }else { + pre.next = newNode; + } + size++; + }else if (index == size){ + addAtTail(val); + } + } + + /** Delete the index-th node in the linked list, if the index is valid. */ + public void deleteAtIndex(int index) { + if (index >= 0 && index < size){ + Node node = node(index); + final Node pre = node.prev; + final Node next = node.next; + + if (pre == null){ + first = next; + }else { + pre.next = next; + node.prev = null; + } + if (next == null) { + last = pre; + } else { + next.prev = pre; + node.next = null; + } + node = null; + size--; + } + } +} +``` + +#### [705. 设计哈希集合](https://leetcode-cn.com/problems/design-hashset/) + +```java +class MyHashSet { + + private int[] mySet = new int[1000001]; + + /** Initialize your data structure here. */ + public MyHashSet() { + + } + + public void add(int key) { + if (key != 0) { + mySet[key] = key; + } else { + mySet[key] = -1; + } + } + + public void remove(int key) { + mySet[key] = 0; + } + + /** Returns true if this set contains the specified element */ + public boolean contains(int key) { + if (key != 0) { + return mySet[key] != 0; + } else { + return mySet[key] == -1; + } + } +} +``` + +#### [641. 设计循环双端队列](https://leetcode-cn.com/problems/design-circular-deque/) + +```java +class MyCircularDeque { + + private int capacity; + private int[] arr; + private int front; + private int rear; + + /** Initialize your data structure here. Set the size of the deque to be k. */ + public MyCircularDeque(int k) { + capacity = k + 1; + arr = new int[capacity]; + } + + /** Adds an item at the front of Deque. Return true if the operation is successful. */ + public boolean insertFront(int value) { + if (isFull()) { + return false; + } + front = (front - 1 + capacity) % capacity; + arr[front] = value; + 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; + } + arr[rear] = value; + rear = (rear + 1) % capacity; + return true; + } + + /** Deletes an item from the front of Deque. Return true if the operation is successful. */ + public boolean deleteFront() { + if (isEmpty()) { + return false; + } + front = (front + 1) % capacity; + return true; + } + + /** Deletes an item from the rear of Deque. Return true if the operation is successful. */ + public boolean deleteLast() { + if (isEmpty()) { + return false; + } + rear = (rear - 1 + capacity) % capacity; + return true; + } + + /** Get the front item from the deque. */ + public int getFront() { + if (isEmpty()) { + return -1; + } + return arr[front]; + } + + /** Get the last item from the deque. */ + public int getRear() { + if (isEmpty()) { + return -1; + } + // 当 rear 为 0 时防止数组越界 + return arr[(rear - 1 + capacity) % capacity]; + } + + /** Checks whether the circular deque is empty or not. */ + public boolean isEmpty() { + return front == rear; + } + + /** Checks whether the circular deque is full or not. */ + public boolean isFull() { + return (rear + 1) % capacity == front; + } +} +``` +