class MyStack {
Queue<Integer> queue1;
Queue<Integer> queue2;
/** Initialize your data structure here. */
public MyStack() {
queue1 = new LinkedList<Integer>();
queue2 = new LinkedList<Integer>();
}
/** Push element x onto stack. */
public void push(int x) {
queue2.offer(x);
while (!queue1.isEmpty()) {
queue2.offer(queue1.poll());
}
Queue<Integer> temp = queue1;
queue1 = queue2;
queue2 = temp;
}
/** Removes the element on top of the stack and returns that element. */
public int pop() {
return queue1.poll();
}
/** Get the top element. */
public int top() {
return queue1.peek();
}
/** Returns whether the stack is empty. */
public boolean empty() {
return queue1.isEmpty();
}
}
作者:力扣官方题解
链接:https://leetcode.cn/problems/implement-stack-using-queues/solutions/432204/yong-dui-lie-shi-xian-zhan-by-leetcode-solution/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
class MyStack {
Queue<Integer> queue;
public MyStack() {
queue = new LinkedList<>();
}
//每 offer 一个数(A)进来,都重新排列,把这个数(A)放到队列的队首
public void push(int x) {
queue.offer(x);
int size = queue.size();
//移动除了 A 的其它数
while (size-- > 1)
queue.offer(queue.poll());
}
public int pop() {
return queue.poll();
}
public int top() {
return queue.peek();
}
public boolean empty() {
return queue.isEmpty();
}
}
class MyStack {
Queue<Integer> queue;
public MyStack() {
queue = new LinkedList<>();
}
public void push(int x) {
queue.add(x);
}
public int pop() {
rePosition();
return queue.poll();
}
public int top() {
rePosition();
int result = queue.poll();// 将最顶部数据弹出来,重新在放回队列的尾部,方便在pop的时候重新变成栈顶数据
queue.add(result);
return result;
}
public boolean empty() {
return queue.isEmpty();
}
public void rePosition(){
int size = queue.size();
size--;
while(size-->0)
queue.add(queue.poll());
}
}
class MyStack {
Queue<Integer> queue;
public MyStack() {
queue = new LinkedList<Integer>();
}
public void push(int x) {
int n = queue.size();
queue.offer(x);
for(int i = 0; i < n; i++){
queue.offer(queue.poll());
}
}
public int pop() {
return queue.poll();
}
public int top() {
return queue.peek();
}
public boolean empty() {
return queue.isEmpty();
}
}
思路:q1 压入元素,压入时用变量 top 记录栈顶元素;pop() 时将 q1 元素移到 q2,直到 q1 剩1个元素时停止(在移动时用 top 记录最新的栈顶元素),此时该元素即为最新的栈顶元素,记录它并移除元素,然后交换 q1 q2 即可
复杂度:时间复杂度pop O(n)、其他O(1);空间复杂度O(n)
class MyStack {
private Queue q1 = new LinkedList<>();
private Queue q2 = new LinkedList<>();
private int top;
public MyStack() {
q1 = new LinkedList<>();
q2 = new LinkedList<>();
}
public void push(int x) {
q1.push(x);
top = x;
}
public int pop() {
int res;
while (q1.size() > 1) {
top = q1.remove();
q2.add(top);
}
res = q1.remove();
Queue tmp = q1;
q1 = q2;
q2 = tmp;
return res;
}
public int top() {
return top;
}
public boolean empty() {
return q1.isEmpty();
}
}
class MyStack(object):
def __init__(self):
self._queue = collections.deque()
def push(self, x):
"""
:type x: int
:rtype: None
"""
q = self._queue
q.append(x)
for _ in range(len(q)-1):
q.append(q.popleft())
def pop(self):
"""
:rtype: int
"""
return self._queue.popleft()
def top(self):
"""
:rtype: int
"""
if len(self._queue)==0:
return None
else:
return self._queue[0]
def empty(self):
"""
:rtype: bool
"""
return len(self._queue)==0
The key of this problem is that queue doesn’t have pop only has popleft. So we need to make the last push thing at first. Every time we push we need to get all of elements out.
Space: O(N)
Compute:O(N) 一
class MyStack {
Queue<Integer> queue;
public MyStack() {
queue = new LinkedList<>();
}
public void push(int x) {
int n = queue.size();
queue.offer(x);
for (int i = 0; i < n; i++) {
queue.offer(queue.poll());
}
}
public int pop() {
return queue.poll();
}
public int top() {
return queue.peek();
}
public boolean empty() {
if(queue.size() == 0){
return true;
}
return false;
}
}
class MyStack {
private Queue<Integer> queue;
public MyStack() {
queue = new LinkedList<Integer>();
}
public void push(int x) {
int num = queue.size();
queue.add(x);
for (int i = 0; i < num; i++) {
queue.add(queue.remove());
}
}
public int pop() {
return queue.remove();
}
public int top() {
return queue.peek();
}
public boolean empty() {
return queue.isEmpty();
}
}
共以下 31 个回答
#include
#include
using namespace std;
class Stack {
public:
// 判断栈是否为空
bool isEmpty() {
return top -1;
}
private:
int top; // 栈顶指针
int maxSize; // 栈的最大容量
int* stackArray; // 栈数组
};
时间复杂度O(1),空间复杂度o(n)
不太明白这个有啥意义
思路
解题方法1
时间复杂度: $O(n)$
空间复杂度: $O(n)$
解题方法2
#python题解
class MyStack(object):
class MyStack:
Your MyStack object will be instantiated and called as such:
obj = MyStack()
obj.push(x)
param_2 = obj.pop()
param_3 = obj.top()
param_4 = obj.empty()
时间复杂度:O(N+1)
空间复杂度:O(N+1)
class MyStack {
public:
MyStack() {
private: resque, tempque; // tempque用于临时存放要反转的元素
queue
};
时间复杂度:入栈O(n),其他O(1),空间复杂度:O(n)
//栈,先入后出; queue1; queue2;
//队列,先入先出;
//插入的复杂度为o(1);
//删除的复杂度为o(n);
//空间的复杂度为o(n);
class MyStack {
public:
queue
queue
};
时间复杂度:入栈为O(n),其余都是 O(1) queue1;
空间复杂度:O(n)
class MyStack {
public:
queue
MyStack() {
};
pop和top的时间O1,push On
push操作时间复杂度O(n),空间复杂度O(1)
pop操作时间复杂度O(1),空间复杂度O(1)
top操作时间复杂度O(1),空间复杂度O(1)
empty操作时间复杂度O(n),空间复杂度O(1)
思路:两个队列,用另一个队列获取最后一个元素即可
时间复杂度O(N),空间复杂度O(N)
后来想了想确实一个队列就能实现
两个队列反而麻烦了
一个队列
”’java q1; q2;
//两个队列模拟栈
import java.util.Queue;
import java.util.LinkedList;
class MyStack {
public Queue
public Queue
}
时间O(n) 空间O(n)
The key of this problem is that queue doesn’t have pop only has popleft. So we need to make the last push thing at first. Every time we push we need to get all of elements out.
Space: O(N)
Compute:O(N) 一
1、一个进去之后,前面的都出队插入队尾
添加元素在一个队列,弹出元素时,先将前面元素放入另一个队列保存,弹出完毕,将另一个队列中的元素返回第一个队列。
始终保持队列的第一个元素是待出栈的元素
“`python
from collections import deque
class MyStack:
<
pre>
def init(self):self.inq = deque()
self.outq = deque()
def push(self, x: int) -> None:
self.inq.append(x)
def pop(self) -> int:
while len(self.inq)>1:
self.outq.append(self.inq.popleft())
tmp = self.inq.popleft()
self.inq,self.outq = self.outq,self.inq #一开始一直想的是挪元素,看了代码才发现挪来挪去其实值的顺序没变化的,可以直接赋值
def top(self) -> int:
#先获取到顶元素,此时inq为空
while len(self.inq)>1:
self.outq.append(self.inq.popleft())
tmp = self.inq.popleft()
#然后将这个元素再入栈
self.outq.append(tmp)
#最后再互换两个队列
self.outq, self.inq = self.inq, self.outq
def empty(self) -> bool:
if len(self.inq)0:
return True
else:
return False
```python
push: 時間複雜度 O(n), 空間複雜度 O(n)
pop, top, empty: 時間複雜度 O(1), 空間複雜度 O(1)