题目[题目及部分解法来自力扣]
题目一:
用两个栈实现一个队列。队列的声明如下,请实现它的两个函数 appendTail 和 deleteHead ,分别完成在队列尾部插入整数和在队列头部删除整数的功能。(若队列中没有元素,deleteHead 操作返回 -1 )
solution one:
两个栈实现队列:栈是FILO原则,而队列是FIFO原则。所以两个栈实现队列,先push进stack_in,之后再stack_out中pop,这样就达到了FIFO的效果。但是如果stack_out为空而stack_in不为空时,则需要将stack_in中元素依次pop,再push进stack_out。再依次进行pop();
class CQueue {
Stack<Integer> stack_in;
Stack<Integer> stack_out;
public CQueue() {
this.stack_in = new Stack<Integer>();
this.stack_out = new Stack<Integer>();
}
//队列末尾插入整数
public void appendTail(int value) {
stack_in.push(value);
}
//队列头部删除整数
public int deleteHead() {
if(stack_in.empty() && stack_out.empty())
return -1;
if( !stack_out.empty() ){
int temp = stack_out.pop();
return temp;
}
else{
while(!stack_in.empty()){
int temp1 = stack_in.pop();
stack_out.push(temp1);
}
}
int temp3 = stack_out.pop();
return temp3;
}
}
solution two:
思想都是一个,这里给出使用LinkedList代替栈的写法:class CQueue {
LinkedList<Integer> A, B;
public CQueue() {
A = new LinkedList<Integer>();
B = new LinkedList<Integer>();
}
public void appendTail(int value) {
A.addLast(value);
}
public int deleteHead() {
if(!B.isEmpty()) return B.removeLast();
if(A.isEmpty()) return -1;
while(!A.isEmpty())
B.addLast(A.removeLast());
return B.removeLast();
}
}
队列(Queue):
- LinkedList类实现了Queue接口,因此我们可以把LinkedList当成Queue来用。
- offer,add 区别:
一些队列有大小限制,因此如果想在一个满的队列中加入一个新项,多出的项就会被拒绝。这时新的 offer 方法就可以起作用了。它不是对调用 add() 方法抛出一个 unchecked 异常,而只是得到由 offer() 返回的 false。 - poll,remove 区别:
remove() 和 poll() 方法都是从队列中删除第一个元素。remove() 的行为与 Collection 接口的版本相似, 但是新的 poll() 方法在用空集合调用时不是抛出异常,只是返回 null。因此新的方法更适合容易出现异常条件的情况。 - peek,element区别:
element() 和 peek() 用于在队列的头部查询元素。与 remove() 方法类似,在队列为空时, element() 抛出一个异常,而 peek() 返回 null。
>
题目二:
定义栈的数据结构,请在该类型中实现一个能够得到栈的最小元素的 min 函数在该栈中,调用 min、push 及 pop 的时间复杂度都是 O(1)。 > 
solution one:
使用 java LinkedList 来进行操作。 **attention:LinkedList 本身没有 isEmpty()方法,但是父类 java.util.AbstractCollection.isEmpty()是有的!!!!**class MinStack {
LinkedList<Integer> Stack;
LinkedList<Integer> minStack;
/** initialize your data structure here. */
public MinStack() {
Stack = new LinkedList<Integer>();
minStack = new LinkedList<Integer>();
}
public void push(int x) {
Stack.addLast(x);
if(minStack.isEmpty() || minStack.getFirst() >= x)
minStack.addFirst(x);
}
public void pop() {
if(Stack.removeLast().equals(minStack.getFirst()))
minStack.removeFirst();
}
public int top() {
return Stack.getLast();
}
public int min() {
return minStack.getFirst();
}
}
solution two:
使用 Stack 来进行操作。 **attention:stack本身没有add()方法,但是继承的类vector有add方法同样vector的父类和实现接口List同样有add()方法。!!!**class MinStack {
Stack<Integer> Stack;
Stack<Integer> minStack;
/** initialize your data structure here. */
public MinStack() {
Stack = new Stack<Integer>();
minStack = new Stack<Integer>();
}
public void push(int x) {
Stack.add(x);
if(minStack.isEmpty() || minStack.peek() >= x)
minStack.add(x);
}
public void pop() {
if(Stack.pop().equals(minStack.peek()))
minStack.pop();
}
public int top() {
return Stack.peek();
}
public int min() {
return minStack.peek();
}
}