public static class MyStack2 { private Stack<Integer> stackData; private Stack<Integer> stackMin;
public MyStack2() { stackData = new Stack<Integer>(); stackMin = new Stack<Integer>(); }
public void push(int newNum) { if (stackMin.isEmpty()) { stackMin.push(newNum); } else if (newNum < this.getmin()) { this.stackMin.push(newNum); } else { int newMin = this.stackMin.peek(); this.stackMin.push(newMin); } this.stackData.push(newNum); }
public int pop() { if (this.stackData.isEmpty()) { throw new RuntimeException("Your stack is empty."); } this.stackMin.pop(); return this.stackData.pop(); }
public int getmin() { if (this.stackMin.isEmpty()) { throw new RuntimeException("Your stack is empty."); } return this.stackMin.peek(); } }
public static class TwoStacksQueue { public Stack<Integer> stackPush; public Stack<Integer> stackPop;
public TwoStacksQueue() { stackPush = new Stack<Integer>(); stackPop = new Stack<Integer>(); }
public void add(int pushInt) { stackPush.push(pushInt); }
public int poll() { if (stackPop.empty() && stackPush.empty()) { throw new RuntimeException("Queue is empty!"); } else if (stackPop.empty()) { while (!stackPush.empty()) { stackPop.push(stackPush.pop()); } } return stackPop.pop(); }
public int peek() { if (stackPop.empty() && stackPush.empty()) { throw new RuntimeException("Queue is empty!"); } else if (stackPop.empty()) { while (!stackPush.empty()) { stackPop.push(stackPush.pop()); } } return stackPop.peek(); } }
public static int getAndRemoveLastElement(Stack<Integer> stack) { int result = stack.pop(); if (stack.isEmpty()) { return result; } else { int last = getAndRemoveLastElement(stack); stack.push(result); return last; } }
public static void reverse(Stack<Integer> stack) { if (stack.isEmpty()) { return; } int i = getAndRemoveLastElement(stack); reverse(stack); stack.push(i); }
public static class DogCatQueue { private Queue<PetEnterQueue> dogQ; private Queue<PetEnterQueue> catQ; private long count;
public DogCatQueue() { this.dogQ = new LinkedList<PetEnterQueue>(); this.catQ = new LinkedList<PetEnterQueue>(); this.count = 0; }
public void add(Pet pet) { if (pet.getPetType().equals("dog")) { this.dogQ.add(new PetEnterQueue(pet, this.count++)); } else if (pet.getPetType().equals("cat")) { this.catQ.add(new PetEnterQueue(pet, this.count++)); } else { throw new RuntimeException("err, not dog or cat"); } }
public Pet pollAll() { if (!this.dogQ.isEmpty() && !this.catQ.isEmpty()) { if (this.dogQ.peek().getCount() < this.catQ.peek().getCount()) { return this.dogQ.poll().getPet(); } else { return this.catQ.poll().getPet(); } } else if (!this.dogQ.isEmpty()) { return this.dogQ.poll().getPet(); } else if (!this.catQ.isEmpty()) { return this.catQ.poll().getPet(); } else { throw new RuntimeException("err, queue is empty!"); } }
public Dog pollDog() { if (!this.isDogQueueEmpty()) { return (Dog) this.dogQ.poll().getPet(); } else { throw new RuntimeException("Dog queue is empty!"); } }
public Cat pollCat() { if (!this.isCatQueueEmpty()) { return (Cat) this.catQ.poll().getPet(); } else throw new RuntimeException("Cat queue is empty!"); }
public boolean isEmpty() { return this.dogQ.isEmpty() && this.catQ.isEmpty(); }
public boolean isDogQueueEmpty() { return this.dogQ.isEmpty(); }
public boolean isCatQueueEmpty() { return this.catQ.isEmpty(); }
public static void sortStackByStack(Stack<Integer> stack) { Stack<Integer> help = new Stack<Integer>(); while (!stack.isEmpty()) { int cur = stack.pop(); while (!help.isEmpty() && help.peek() < cur) { stack.push(help.pop()); } help.push(cur); } while (!help.isEmpty()) { stack.push(help.pop()); } }
public static int[] getMaxWindow(int[] arr, int w) { if (arr == null || w < 1 || arr.length < w) { return null; } LinkedList<Integer> qmax = new LinkedList<Integer>(); int[] res = new int[arr.length - w + 1]; int index = 0; for (int i = 0; i < arr.length; i++) { while (!qmax.isEmpty() && arr[qmax.peekLast()] <= arr[i]) { qmax.pollLast(); } qmax.addLast(i); if (qmax.peekFirst() == i - w) { qmax.pollFirst(); } if (i >= w - 1) { res[index++] = arr[qmax.peekFirst()]; } } return res; }
代码如上。 8,构造数组的MaxTree 题目:定义二叉树结构如下:
1 2 3 4 5 6 7 8 9
public static class Node { public int value; public Node left; public Node right;