class WordDictionary { private TreeNode root; public static class TreeNode{ private char data; private TreeNode[] child = new TreeNode[26]; private boolean isEndingChar = false; public TreeNode(char data){ this.data = data; } } /** Initialize your data structure here. */ public WordDictionary() { root = new TreeNode('/'); } /** Adds a word into the data structure. */ public void addWord(String word) { TreeNode node = root; char[] array = word.toCharArray(); for(int i = 0 ;i < array.length ; i++){ int index = array[i] - 'a'; if(node.child[index] == null){ node.child[index] = new TreeNode(array[i]); } node = node.child[index]; } node.isEndingChar = true; } /** Returns if the word is in the data structure. A word could contain the dot character '.' to represent any one letter. */ public boolean search(String word) { if(word == null || word.length() == 0){ return true; } TreeNode node = root; char[] array = word.toCharArray(); return helper(array,0,node); } public boolean helper(char[] array,int index,TreeNode node){ if(index == array.length){ return node.isEndingChar; } char data = array[index]; if(data == '.'){ TreeNode[] child = node.child; for(int i = 0 ; i< child.length ; i++){ if(child[i] != null && helper(array, index+1, child[i])){ return true; } } return false; }else{ int num = data - 'a'; if(node.child[num] == null){ return false; } return helper(array,index+1, node.child[num]); } } }
/** * Your WordDictionary object will be instantiated and called as such: * WordDictionary obj = new WordDictionary(); * obj.addWord(word); * boolean param_2 = obj.search(word); */
/** * Linked list node class */ static class Node<E> { E item;
/** * One of: * - the real successor Node * - this Node, meaning the successor is head.next * - null, meaning there is no successor (this is the last node) */ Node<E> next;
/** Lock held by take, poll, etc */ private final ReentrantLock takeLock = new ReentrantLock(); /** Wait queue for waiting takes */ private final Condition notEmpty = takeLock.newCondition();
/** Lock held by put, offer, etc */ private final ReentrantLock putLock = new ReentrantLock();
/** Wait queue for waiting puts */ private final Condition notFull = putLock.newCondition();
/** * Creates a {@code LinkedBlockingQueue} with a capacity of * {@link Integer#MAX_VALUE}. */ public LinkedBlockingQueue() { this(Integer.MAX_VALUE); }
/** * Creates a {@code LinkedBlockingQueue} with the given (fixed) capacity. * * @param capacity the capacity of this queue * @throws IllegalArgumentException if {@code capacity} is not greater * than zero */ public LinkedBlockingQueue(int capacity) { if (capacity <= 0) throw new IllegalArgumentException(); this.capacity = capacity; last = head = new Node<E>(null); }
/** * Inserts the specified element at the tail of this queue, waiting if * necessary for space to become available. * * @throws InterruptedException {@inheritDoc} * @throws NullPointerException {@inheritDoc} */ public void put(E e) throws InterruptedException { if (e == null) throw new NullPointerException(); // Note: convention in all put/take/etc is to preset local var // holding count negative to indicate failure unless set. int c = -1; Node<E> node = new Node<E>(e); final ReentrantLock putLock = this.putLock; final AtomicInteger count = this.count; putLock.lockInterruptibly(); try { /* * Note that count is used in wait guard even though it is * not protected by lock. This works because count can * only decrease at this point (all other puts are shut * out by lock), and we (or some other waiting put) are * signalled if it ever changes from capacity. Similarly * for all other uses of count in other wait guards. */ while (count.get() == capacity) { notFull.await(); } enqueue(node); c = count.getAndIncrement(); if (c + 1 < capacity) notFull.signal(); } finally { putLock.unlock(); } if (c == 0) signalNotEmpty(); } /** * Links node at end of queue. * * @param node the node */ private void enqueue(Node<E> node) { // assert putLock.isHeldByCurrentThread(); // assert last.next == null; last = last.next = node; }
public E take() throws InterruptedException { E x; int c = -1; final AtomicInteger count = this.count; final ReentrantLock takeLock = this.takeLock; takeLock.lockInterruptibly(); try { while (count.get() == 0) { notEmpty.await(); } x = dequeue(); c = count.getAndDecrement(); if (c > 1) notEmpty.signal(); } finally { takeLock.unlock(); } if (c == capacity) signalNotFull(); return x; } /** * Removes a node from head of queue. * * @return the node */ private E dequeue() { // assert takeLock.isHeldByCurrentThread(); // assert head.item == null; Node<E> h = head; Node<E> first = h.next; h.next = h; // help GC head = first; E x = first.item; first.item = null; return x; }
class Trie { private TreeNode root; public static class TreeNode{ private char data; private TreeNode[] child = new TreeNode[26]; private boolean isEndingChar = false; public TreeNode(char data){ this.data = data; } } /** Initialize your data structure here. */ public Trie() { root = new TreeNode('/'); } /** Inserts a word into the trie. */ public void insert(String word) { TreeNode node = root; char[] array = word.toCharArray(); for(int i = 0 ; i < array.length ; i++){ int index = array[i] - 'a'; if(node.child[index] == null){ node.child[index] = new TreeNode(array[i]); } node = node.child[index]; } node.isEndingChar = true; } /** Returns if the word is in the trie. */ public boolean search(String word) { TreeNode node = root; char[] array = word.toCharArray(); for(int i = 0 ; i < array.length ; i++){ int index = array[i] - 'a'; if(node.child[index] == null){ return false; } node = node.child[index]; } if(!node.isEndingChar){ return false; } return true; } /** Returns if there is any word in the trie that starts with the given prefix. */ public boolean startsWith(String prefix) { TreeNode node = root; char[] array = prefix.toCharArray(); for(int i = 0 ; i < array.length ; i++){ int index = array[i] - 'a'; if(node.child[index] == null){ return false; } node = node.child[index]; } return true; } }
/** * Your Trie object will be instantiated and called as such: * Trie obj = new Trie(); * obj.insert(word); * boolean param_2 = obj.search(word); * boolean param_3 = obj.startsWith(prefix); */
/** * Creates a new {@code DelayQueue} that is initially empty. */ public DelayQueue() {}
构造器为空
1 2 3 4 5 6 7 8 9 10
private final transient ReentrantLock lock = new ReentrantLock(); private final PriorityQueue<E> q = new PriorityQueue<E>(); private Thread leader = null;
/** * Condition signalled when a newer element becomes available * at the head of the queue or a new thread may need to * become leader. */ private final Condition available = lock.newCondition();
/** * Inserts the specified element into this delay queue. * * @param e the element to add * @return {@code true} * @throws NullPointerException if the specified element is null */ public boolean offer(E e) { final ReentrantLock lock = this.lock; lock.lock(); try { q.offer(e); if (q.peek() == e) { leader = null; available.signal(); } return true; } finally { lock.unlock(); } }
/** * Retrieves and removes the head of this queue, waiting if necessary * until an element with an expired delay is available on this queue. * * @return the head of this queue * @throws InterruptedException {@inheritDoc} */ public E take() throws InterruptedException { final ReentrantLock lock = this.lock; lock.lockInterruptibly(); try { for (;;) { E first = q.peek(); if (first == null) available.await(); else { long delay = first.getDelay(NANOSECONDS); if (delay <= 0) return q.poll(); first = null; // don't retain ref while waiting if (leader != null) available.await(); else { Thread thisThread = Thread.currentThread(); leader = thisThread; try { available.awaitNanos(delay); } finally { if (leader == thisThread) leader = null; } } } } } finally { if (leader == null && q.peek() != null) available.signal(); lock.unlock(); } }
/** * Synchronization control For CountDownLatch. * Uses AQS state to represent count. */ private static final class Sync extends AbstractQueuedSynchronizer { private static final long serialVersionUID = 4982264981922014374L;
protected boolean tryReleaseShared(int releases) { // Decrement count; signal when transition to zero for (;;) { int c = getState(); if (c == 0) return false; int nextc = c-1; if (compareAndSetState(c, nextc)) return nextc == 0; } } }