# LeetCode算法题-Implement Queue Using Stacks（Java实现）

### 01 看题和准备

push（x） - 将元素x推送到队列的后面。

pop（） - 从队列前面删除元素。

peek（） - 获取前面的元素。

empty（） - 返回队列是否为空。

MyQueue queue = new MyQueue（）;

queue.push（1）;

queue.push（2）;

queue.peek（）; //返回1

queue.pop（）; //返回1

queue.empty（）; //返回false

### 02 第一种解法

class MyQueue {

private Stack<Integer> stack;

/** Initialize your data structure here. */
public MyQueue() {
stack = new Stack<Integer>();
}

/** Push element x to the back of queue. */
public void push(int x) {
stack.push(x);
}

/** Removes the element from in front of queue and returns that element. */
public int pop() {
Stack<Integer> temp = new Stack<Integer>();
while (!stack.isEmpty()) {
temp.push(stack.pop());
}
int tem = temp.pop();
while (!temp.isEmpty()) {
stack.push(temp.pop());
}
return tem;
}

/** Get the front element. */
public int peek() {
Stack<Integer> temp = new Stack<Integer>();
while (!stack.isEmpty()) {
temp.push(stack.pop());
}
int tem = temp.peek();
while (!temp.isEmpty()) {
stack.push(temp.pop());
}
return tem;
}

/** Returns whether the queue is empty. */
public boolean empty() {
return stack.isEmpty();
}
}


### 03 第二种解法

class MyQueue2 {
Stack<Integer> stack;

/** Initialize your data structure here. */
public MyQueue2() {
stack = new Stack<Integer>();
}

/** Push element x to the back of queue. */
public void push(int x) {
Stack<Integer> temp=new Stack<>();
while(!stack.isEmpty()){
temp.push(stack.pop());
}
temp.push(x);
while(!temp.isEmpty()){
stack.push(temp.pop());
}
}

/** Removes the element from in front of queue and returns that element. */
public int pop() {
return stack.pop();
}

/** Get the front element. */
public int peek() {
int a = stack.pop();
stack.push(a);
return a;
}

/** Returns whether the queue is empty. */
public boolean empty() {
return stack.isEmpty();
}
}


### 04 第三种解法

class MyQueue3 {

private Stack<Integer> s1;
private Stack<Integer> s2;

/** Initialize your data structure here. */
public MyQueue3() {
s1 = new Stack<Integer>();
s2 = new Stack<Integer>();
}

/** Push element x to the back of queue. */
public void push(int x) {
while(!s2.empty()) {
s1.push(s2.pop());
}
s1.push(x);
}

/** Removes the element from in front of queue and returns that element. */
public int pop() {
if (s2.empty()) {
while (!s1.empty()) {
s2.push(s1.pop());
}
}
return s2.pop();
}

/** Get the front element. */
public int peek() {
if (s2.empty()) {
while (!s1.empty()) {
s2.push(s1.pop());
}
}
return s2.peek();
}

/** Returns whether the queue is empty. */
public boolean empty() {
return (s1.empty() && s2.empty());
}
}


### 05 第四种解法

class MyQueue4 {

private Stack<Integer> stack;

/** Initialize your data structure here. */
public MyQueue4() {
stack = new Stack<Integer>();
}

/** Push element x to the back of queue. */
public void push(int x) {
}

/** Removes the element from in front of queue and returns that element. */
public int pop() {
return stack.pop();
}

/** Get the front element. */
public int peek() {
return stack.peek();
}

/** Returns whether the queue is empty. */
public boolean empty() {
return stack.isEmpty();
}
}


### 06 小结

posted @ 2018-12-08 13:54  程序员小川  阅读(137)  评论(0编辑  收藏