princeton algorithms part 1 第二周作业题
2018-01-08 21:11
381 查看
Deque
RandomizedQueue
Permutation
import java.util.Iterator; public class Deque<Item> implements Iterable<Item> { private int N; private Node first; private Node last; private class Node { private Item node; private Node next; private Node past; } public Deque() { first = null; last = null; N = 0; } // construct an empty deque public boolean isEmpty() { return N == 0; } // is the deque empty? public int size() { return N; } // return the number of items on the deque public void addFirst(Item item) { if(item == null) throw new java.lang.IllegalArgumentException(); Node temp = first; first = new Node(); first.node = item; first.next = temp; if (isEmpty()) last = first; else temp.past = first; N++; } // add the item to the front public void addLast(Item item) { if(item == null) throw new java.lang.IllegalArgumentException(); Node temp = last; last = new Node(); last.node = item; last.past = temp; if (isEmpty()) first = last; else temp.next = last; N++; } // add the item to the end public Item removeFirst() { if(isEmpty()) throw new java.util.NoSuchElementException("Queue underflow"); Item temp = first.node; first = first.next; N--; if(isEmpty()) last=null; else first.past = null; return temp; } // remove and return the item from the front public Item removeLast() { if(isEmpty()) throw new java.util.NoSuchElementException("Queue underflow"); Item temp = last.node; last = last.past; N--; if(isEmpty()) first=null; else last.next = null; return temp; } // remove and return the item from the end public Iterator<Item> iterator() { return new ListIterator(); } // return an iterator over items in order from front to end private class ListIterator implements Iterator<Item> { private Node current = first; public boolean hasNext() { return current != null; } public Item next() { if(!hasNext()) throw new java.util.NoSuchElementException(); Item item = current.node; current = current.next; return item; } public void remove() { throw new java.lang.UnsupportedOperationException(); } } }
RandomizedQueue
import edu.princeton.cs.algs4.StdRandom; import java.util.Iterator; import edu.princeton.cs.algs4.StdOut; public class RandomizedQueue<Item> implements Iterable<Item> { private int head, tail; private Item[] queue; private int N; public RandomizedQueue() { queue = (Item[]) new Object[1]; head = 0; tail = 0; N = 0; }// construct an empty randomized queue public boolean isEmpty() { return N == 0; } // is the randomized queue empty? public int size() { return N; }// return the number of items on the randomized queue private void resize(int max) { Item[] temp = (Item[]) new Object[max]; for(int i = 0; i < N; i++) temp[i] = queue[(head + i) % queue.length]; queue = temp; head = 0; tail = N; } public void enqueue(Item item) { if(item == null) throw new java.lang.IllegalArgumentException(); if(N == queue.length) resize(2*queue.length); queue[tail++] = item; if (tail == queue.length) tail = 0; N++; }// add the item public Item dequeue() { if (isEmpty()) throw new java.util.NoSuchElementException(); int index = (head + StdRandom.uniform(N)) % queue.length; Item item = queue[index]; if (tail == 0) { tail = queue.length - 1; queue[index] = queue[tail]; queue[tail] = null; } else { queue[index] = queue[--tail]; queue[tail] = null; } N--; if(N>0 && N==queue.length/4) resize(queue.length/2); return item; } // remove and return a random item public Item sample() { if (isEmpty()) throw new java.util.NoSuchElementException(); int index = (head + StdRandom.uniform(N)) % queue.length; return queue[index]; }// return a random item (but do not remove it) public Iterator<Item> iterator() { return new RQIterator(); } // return an independent iterator over items in random order private class RQIterator implements Iterator<Item> { private int index = 0; private Item[] r; public RQIterator() { r = (Item[]) new Object ; for(int i=0; i<N; i++) r[i] = queue[i]; StdRandom.shuffle(r); } public boolean hasNext() { return index < N; } public void remove() { throw new java.lang.UnsupportedOperationException(); } public Item next() { if(!hasNext()) throw new java.util.NoSuchElementException(); Item item = r[index++]; return item; } } }
Permutation
import edu.princeton.cs.algs4.StdIn; import edu.princeton.cs.algs4.StdOut; public class Permutation { public static void main(String[] args) { RandomizedQueue<String> queue = new RandomizedQueue<String>(); int num = Integer.parseInt(args[0]); while (!StdIn.isEmpty()) { String item = StdIn.readString(); queue.enqueue(item); } for (String s : queue) { if(num == 0) break; StdOut.println(s); num--; } } }
相关文章推荐
- princeton algorithms part 1 第一周作业题
- princeton algorithms part 1 第三周作业题
- 【Java、算法】Princeton Algorithms Part I Week 1 Exercise
- Week 1 Assignment - Wordnet - Princeton - Algorithms Part II
- princeton algorithms part 1 第四周作业题
- Stanford - Algorithms: Design and Analysis, Part 2 - Week 2 Assignment: Clustering
- c# 调用MD5CryptoServiceProvider出现 System.InvalidOperationException: This implementation is not part of the Windows Platform FIPS validated cryptographic algorithms.
- Union-find(Algorithms, Part 1, week 1 )
- Internal Sorting Algorithms Part 1/2: Elementary Sorts
- Algorithms: Design and Analysis, Part 1, Programming Assignment #5
- 异常:System.InvalidOperationException: This implementation is not part of the Windows Platform FIPS validated cryptographic algorithms 这个实现是不是Windows平台FIPS验证的加密算法。解决方法
- 自主学习报告第二周F_Part
- This implementation is not part of the Windows Platform FIPS validated cryptographic algorithms.
- Coursare·Algorithms, Part I第二周作业
- This implementation is not part of the Windows Platform FIPS validated cryptographic algorithms
- Internal Sorting Algorithms Part 2/2: Advanced Sorts
- RTB撕开黑盒子 Part 1: Datacratic's RTB Algorithms
- This implementation is not part of the Windows Platform FIPS validated cryptographic algorithms
- Stanford - Algorithms: Design and Analysis, Part 1 - Week 1 Assignment: number of inversions
- Starting with Algorithms(Part 1)