Implement Queue using Stacks
2017-08-18 00:00
106 查看
问题:
Implement the following operations of a queue using stacks.
push(x) -- Push element x to the back of queue.
pop() -- Removes the element from in front of queue.
peek() -- Get the front element.
empty() -- Return whether the queue is empty.
Notes:
You must use only standard operations of a stack -- which means only
Depending on your language, stack may not be supported natively. You may simulate a stack by using a list or deque (double-ended queue), as long as you use only standard operations of a stack.
You may assume that all operations are valid (for example, no pop or peek operations will be called on an empty queue).
解决:
【注】关于测试参数的解释:
["MyQueue","push","push","peek","pop","pop","empty"] --- 表示要进行的操作
[[],[1],[2],[],[],[],[]] --- 表示对应传入的参数
① 使用两个栈s1,s2,进栈的时候不做任何处理,出栈的时候把栈逆序放在另外一个栈,出另外一个栈。
之前写经常出现空栈异常或者超时,就是没有写好s1,s2之间的转换。
class MyQueue { // 87ms
Stack<Integer> s1 = new Stack<>();
Stack<Integer> s2 = new Stack<>();
/** Initialize your data structure here. */
public MyQueue() {}
/** Push element x to the back of queue. */
public void push(int x) {
while(! s2.isEmpty()) s1.push(s2.pop());
s1.push(x);
}
/** Removes the element from in front of queue and returns that element. */
public int pop() {
while(! s1.isEmpty()) s2.push(s1.pop());
return s2.pop();
}
/** Get the front element. */
public int peek() {
while(! s1.isEmpty()) s2.push(s1.pop());
return s2.peek();
}
/** Returns whether the queue is empty. */
public boolean empty() {
while(! s2.isEmpty()) s1.push(s2.pop());
return s1.isEmpty();
}
}
/**
* Your MyQueue object will be instantiated and called as such:
* MyQueue obj = new MyQueue();
* obj.push(x);
* int param_2 = obj.pop();
* int param_3 = obj.peek();
* boolean param_4 = obj.empty();
*/
② 使用两个栈,在进栈的时候,把栈逆序放在另外一个栈,出的时候直接出另外一个栈就可以了。
class MyQueue { //93ms
Stack<Integer> s1 = new Stack<>();
Stack<Integer> s2 = new Stack<>();
/** Initialize your data structure here. */
public MyQueue() {}
/** Push element x to the back of queue. */
public void push(int x) {
while(! s1.isEmpty()) s2.push(s1.pop());
s2.push(x);
while(! s2.isEmpty()) s1.push(s2.pop());
}
/** Removes the element from in front of queue and returns that element. */
public int pop() {
return s1.pop();
}
/** Get the front element. */
public int peek() {
return s1.peek();
}
/** Returns whether the queue is empty. */
public boolean empty() {
return s1.isEmpty();
}
}
③ 除了使用两个栈之外,额外使用一个指针指向头部。
class MyQueue { //113ms
Stack<Integer> s1 = new Stack<>();
Stack<Integer> s2 = new Stack<>();
int head;
/** Initialize your data structure here. */
public MyQueue() {}
/** Push element x to the back of queue. */
public void push(int x) {
if(s1.isEmpty()) head = x;
s1.push(x);
}
/** Removes the element from in front of queue and returns that element. */
public int pop() {
while(! s1.isEmpty()) s2.push(s1.pop());
int top = -1;
if(! s2.isEmpty()){
top = s2.pop();
if(! s2.isEmpty()) head = s2.peek();
}
while(! s2.isEmpty()) s1.push(s2.pop());
return top;
}
/** Get the front element. */
public int peek() {
return head;
}
/** Returns whether the queue is empty. */
public boolean empty() {
return s1.isEmpty();
}
}
Implement the following operations of a queue using stacks.
push(x) -- Push element x to the back of queue.
pop() -- Removes the element from in front of queue.
peek() -- Get the front element.
empty() -- Return whether the queue is empty.
Notes:
You must use only standard operations of a stack -- which means only
push to top,
peek/pop from top,
size, and
is emptyoperations are valid.
Depending on your language, stack may not be supported natively. You may simulate a stack by using a list or deque (double-ended queue), as long as you use only standard operations of a stack.
You may assume that all operations are valid (for example, no pop or peek operations will be called on an empty queue).
解决:
【注】关于测试参数的解释:
["MyQueue","push","push","peek","pop","pop","empty"] --- 表示要进行的操作
[[],[1],[2],[],[],[],[]] --- 表示对应传入的参数
① 使用两个栈s1,s2,进栈的时候不做任何处理,出栈的时候把栈逆序放在另外一个栈,出另外一个栈。
之前写经常出现空栈异常或者超时,就是没有写好s1,s2之间的转换。
class MyQueue { // 87ms
Stack<Integer> s1 = new Stack<>();
Stack<Integer> s2 = new Stack<>();
/** Initialize your data structure here. */
public MyQueue() {}
/** Push element x to the back of queue. */
public void push(int x) {
while(! s2.isEmpty()) s1.push(s2.pop());
s1.push(x);
}
/** Removes the element from in front of queue and returns that element. */
public int pop() {
while(! s1.isEmpty()) s2.push(s1.pop());
return s2.pop();
}
/** Get the front element. */
public int peek() {
while(! s1.isEmpty()) s2.push(s1.pop());
return s2.peek();
}
/** Returns whether the queue is empty. */
public boolean empty() {
while(! s2.isEmpty()) s1.push(s2.pop());
return s1.isEmpty();
}
}
/**
* Your MyQueue object will be instantiated and called as such:
* MyQueue obj = new MyQueue();
* obj.push(x);
* int param_2 = obj.pop();
* int param_3 = obj.peek();
* boolean param_4 = obj.empty();
*/
② 使用两个栈,在进栈的时候,把栈逆序放在另外一个栈,出的时候直接出另外一个栈就可以了。
class MyQueue { //93ms
Stack<Integer> s1 = new Stack<>();
Stack<Integer> s2 = new Stack<>();
/** Initialize your data structure here. */
public MyQueue() {}
/** Push element x to the back of queue. */
public void push(int x) {
while(! s1.isEmpty()) s2.push(s1.pop());
s2.push(x);
while(! s2.isEmpty()) s1.push(s2.pop());
}
/** Removes the element from in front of queue and returns that element. */
public int pop() {
return s1.pop();
}
/** Get the front element. */
public int peek() {
return s1.peek();
}
/** Returns whether the queue is empty. */
public boolean empty() {
return s1.isEmpty();
}
}
③ 除了使用两个栈之外,额外使用一个指针指向头部。
class MyQueue { //113ms
Stack<Integer> s1 = new Stack<>();
Stack<Integer> s2 = new Stack<>();
int head;
/** Initialize your data structure here. */
public MyQueue() {}
/** Push element x to the back of queue. */
public void push(int x) {
if(s1.isEmpty()) head = x;
s1.push(x);
}
/** Removes the element from in front of queue and returns that element. */
public int pop() {
while(! s1.isEmpty()) s2.push(s1.pop());
int top = -1;
if(! s2.isEmpty()){
top = s2.pop();
if(! s2.isEmpty()) head = s2.peek();
}
while(! s2.isEmpty()) s1.push(s2.pop());
return top;
}
/** Get the front element. */
public int peek() {
return head;
}
/** Returns whether the queue is empty. */
public boolean empty() {
return s1.isEmpty();
}
}
相关文章推荐
- [LeetCode] Implement Queue using Stacks
- Leetcode Implement Queue using Stacks
- Lettcode_232_Implement Queue using Stacks
- Implement Queue using Stacks 解答
- Implement Queue using Stacks
- <LeetCode OJ>Implement Queue using Stacks【232】
- LeetCode_Implement Queue using Stacks_队列操作
- [LeetCode][数据结构]Implement Queue using Stacks
- Implement Queue using Stacks
- leetcode 232 Implement Queue using Stacks C++
- Implement Queue using Stacks
- leetcode 232:Implement Queue using Stacks
- LeetCode Implement Queue using Stacks
- 【LeetCode 232_数据结构_队列_实现】Implement Queue using Stacks
- LeetCode OJ 之 Implement Queue using Stacks(使用栈实现队列)
- Implement Queue using Stacks
- Implement Queue using Stacks
- Leetcode_232_Implement Queue using Stacks
- Leetcode 232 Implement Queue using Stacks
- leetcode oj java Implement Queue using Stacks