class MyStack {
Queue queue1;//对应栈
Queue queue2;//操作队列
public MyStack() {
queue1 = new LinkedList<Integer>();
queue2 = new LinkedList<Integer>();
}
/**2放置新元素,然后1存储整个队列,将1传入2后再传回去 */
public void push(int x) {
queue2.offer(x);
while(!queue1.isEmpty()){
queue2.offer(queue1.poll());
}
//交换队列
Queue<Integer> temp = queue1;
queue1 = queue2;
queue2 = temp;
}
public int pop() {
return queue1.poll();
}
public int top() {
return queue1.peek();
}
public boolean empty() {
return queue1.isEmpty();
}
}
/**
* Your MyStack object will be instantiated and called as such:
* MyStack obj = new MyStack();
* obj.push(x);
* int param_2 = obj.pop();
* int param_3 = obj.top();
* boolean param_4 = obj.empty();
*/
//双队列实现栈
class MyStack {
Queue<Integer> A, B;
public MyStack() {
A = new LinkedList<Integer>();
B = new LinkedList<Integer>();
}
public void push(int x) {
B.add(x);
while(!A.isEmpty()){
B.add(A.remove());
}
Queue<Integer> C = B;
B = A;
A = C;
}
public int pop() {
return A.remove();
}
public int top() {
return A.peek();
}
public boolean empty() {
return A.isEmpty();
}
}
/**
* Your MyStack object will be instantiated and called as such:
* MyStack obj = new MyStack();
* obj.push(x);
* int param_2 = obj.pop();
* int param_3 = obj.top();
* boolean param_4 = obj.empty();
*/
/**
解题思路:采用一个队列,新值入队后,将队列内已有值出队在入队,以做到队头元素为最后加入的元素
时间复杂度O(N),空间复杂度O(N)
ps:真是为了实现而实现
*/
class MyStack {
Deque<Integer> q;
public MyStack() {
q = new ArrayDeque<>();
}
public void push(int x) {
int sz = q.size();
q.add(x);
for (int i = 0; i < sz; i++){
q.add(q.pop());
}
}
public int pop() {
return q.pop();
}
public int top() {
return q.getFirst();
}
public boolean empty() {
return q.isEmpty();
}
}
/**
* Your MyStack object will be instantiated and called as such:
* MyStack obj = new MyStack();
* obj.push(x);
* int param_2 = obj.pop();
* int param_3 = obj.top();
* boolean param_4 = obj.empty();
*/
/**
* Your MyStack object will be instantiated and called as such:
* MyStack* obj = new MyStack();
* obj->push(x);
* int param_2 = obj->pop();
* int param_3 = obj->top();
* bool param_4 = obj->empty();
*/
class MyStack {
Queue<Integer> A;
Queue<Integer> B;
public MyStack() {
A = new LinkedList<Integer>();
B = new LinkedList<Integer>();
}
public void push(int x) {
B.offer(x);
while(!A.isEmpty()){
B.offer(A.poll());
}
Queue<Integer> t = A;
A = B;
B = t;
}
public int pop() {
return A.poll();
}
public int top() {
return A.peek();
}
public boolean empty() {
return A.isEmpty();
}
}
/** 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();
}
}
void push(int x) {
q1.push(x);
}
int pop() {
int size = q1.size();
size--;
while(size--){
q2.push(q1.front());
q1.pop();
}
int result = q1.front();
q1.pop();
q1 = q2;
while(!q2.empty()){
q2.pop();
}
return result;
}
int top() {
return q1.back();
}
bool empty() {
return q1.empty();
}
};
/**
* Your MyStack object will be instantiated and called as such:
* MyStack* obj = new MyStack();
* obj->push(x);
* int param_2 = obj->pop();
* int param_3 = obj->top();
* bool param_4 = obj->empty();
*/
from collections import deque
class MyStack:
def __init__(self):
self.queue1 = deque() # main
self.queue2 = deque() # sub
def push(self, x: int) -> None:
# always keep sub empty as a buffer
# to make sure when push it can be at the top.
self.queue2.append(x)
while len(self.queue1):
self.queue2.append(self.queue1.popleft())
self.queue2, self.queue1 = self.queue1, self.queue2
def pop(self) -> int:
return self.queue1.popleft()
def top(self) -> int:
return self.queue1[0]
def empty(self) -> bool:
return not self.queue1
class MyStack {
public 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() {
return queue.isEmpty();
}
}
class MyStack {
Queue<Integer> queue;
public MyStack() {
queue=new LinkedList<>();
}
public void push(int x) {
queue.add(x);
}
public int pop() {
int size=queue.size();
while(size--!=1){
queue.add(queue.remove());
}
return queue.remove();
}
public int top() {
int size=queue.size();
while(size--!=1){
queue.add(queue.remove());
}
int top=queue.peek();
queue.add(queue.remove());
return top;
}
public boolean empty() {
return queue.isEmpty();
}
}
/**
* Your MyStack object will be instantiated and called as such:
* MyStack obj = new MyStack();
* obj.push(x);
* int param_2 = obj.pop();
* int param_3 = obj.top();
* boolean param_4 = obj.empty();
*/
class MyStack {
Queue<Integer> q1;
Queue<Integer> q2;
public MyStack() {
q1 = new LinkedList<>();
q2 = new LinkedList<Integer>();
}
public void push(int x) {
q1.offer(x);
}
public int pop() {
while(q1.size()>1){
q2.offer(q1.poll());
}
int pop = q1.poll();
while(q2.size()>0){
q1.offer(q2.poll());
}
return pop;
}
public int top() {
while(q1.size()>1){
q2.offer(q1.poll());
}
int top = q1.peek();
q2.offer(q1.poll());
while(q2.size()>0){
q1.offer(q2.poll());
}
return top;
}
public boolean empty() {
if(q1.size()<=0){
return true;
}else{
return false;
}
}
}
/**
* Your MyStack object will be instantiated and called as such:
* MyStack obj = new MyStack();
* obj.push(x);
* int param_2 = obj.pop();
* int param_3 = obj.top();
* boolean param_4 = obj.empty();
*/
class MyStack {
Queue<Integer> in;
Queue<Integer> out;
public MyStack() {
in = new LinkedList<>();
out = new LinkedList<>();
}
public void push(int x) {
// in表示新入的元素
in.offer(x);
// out表示之前存过的元素
while (!out.isEmpty()) {
in.offer(out.poll()); // 把之前存的元素再放到in里的新元素后面,就实现了栈
}
// 互换栈
Queue<Integer> temp = out;
out = in;
in = temp;
}
public int pop() {
return out.poll();
}
public int top() {
return out.peek();
}
public boolean empty() {
return out.isEmpty();
}
}
共以下 35 个回答
class MyStack { queue1;//对应栈 queue2;//操作队列
Queue
Queue
}
/**
* Your MyStack object will be instantiated and called as such:
* MyStack obj = new MyStack();
* obj.push(x);
* int param_2 = obj.pop();
* int param_3 = obj.top();
* boolean param_4 = obj.empty();
*/
###
需要借助一个辅助队列,每次push的时候,先将元素放入,再放入已有的元素。
队列
class MyStack {
public:
MyStack() {
private: m_q1;
std::queue
};
/**
* Your MyStack object will be instantiated and called as such:
* MyStack* obj = new MyStack();
* obj->push(x);
* int param_2 = obj->pop();
* int param_3 = obj->top();
* bool param_4 = obj->empty();
*/
“`cpp main; temp;
//简单题
class MyStack {
queue
queue
public:
MyStack() {
};
“`cpp
代码que1; que2;
class MyStack {
Queue
Queue
public MyStack() {
que1=new LinkedList<>();
que2=new LinkedList<>();
}
//1
//2 1
public void push(int x) {
if(que2.isEmpty()){
que2.offer(x);
while(!que1.isEmpty()){
int val=que1.poll();
que2.offer(val);
}
}else{
que1.offer(x);
while(!que2.isEmpty()){
int val=que2.poll();
que1.offer(val);
}
}
}
“`c++ queue1; queue2;
class MyStack {
public:
queue
queue
};
//用队列实现栈
“`
实现思路: 在入队列时 使用两个队列处理,确保当前元素 处在队列第一个位置中
保持q2有着最后一个到来的元素
“`c++q1; q2;
class MyStack {
public:
queue
queue
MyStack() {
};
“`
注意其中为了泛化而做的一些“额外”操作
思路:和第一道题类似,用两个队列相互导进来导出去来实现对应方法函数
思路:write队列作为临时中转站,负责将【后进】的元素排到队列第一个,进而达到【先出】的效果。再把write队列中所有元素腾到read队列中即可。
核心思想:新的值单独入一个队列,再把另一个队列的值挪过来,实现逆序输出
class MyStack { q1; q2;
Queue
Queue
}
时间:O(1)
空间:O(n)
两个队列 queue1; queue2;
class MyStack {
Queue
Queue
}
//pop操作就是留下最后一个队列元素出列
class MyStack {
打卡 q1; q2;
class MyStack {
public:
queue
queue
MyStack() {
};
/**
* Your MyStack object will be instantiated and called as such:
* MyStack* obj = new MyStack();
* obj->push(x);
* int param_2 = obj->pop();
* int param_3 = obj->top();
* bool param_4 = obj->empty();
*/
用两个队列,一个是缓存原有的数据,另一个是handle新入的数据
push时将新数据放入辅助队列,此时他在队列头部,然后将原有数据依次放入辅助队列,最后将主队列和辅助队列交换
pop时因为新数据在主队列最左边,根据LIFO原则他应该最先出来,所以popleft。
time O(n) space O(n)
思路:每次加入新元素时,都把前方的元素出队列,再进队列,实现队列内的头即为栈顶。
时间复杂度O(N),空间复杂度O(N)。
将队列想象成一个环,要出栈就出队再入队留下原来的第一个元素, 这样队头就是栈顶,但是只能o(n)
时间复杂度O(N) 空间复杂度O(N)
时间复杂度O(n)
空间复杂度O(n)
思路:使用队列来维护栈的性质
1.代码
2.复杂度分析
思路
题目要求使用两个队列实现栈,
que1用于添加元素,que2用于删除元素。构造函数
初始化两个队列。
push 方法
将元素添加至
que1的末尾即可。时间复杂度:O(1),只是将元素添加至
que1的末尾。空间复杂度:O(n),元素个数。
pop 方法
将
que1中除最后一个元素外的其他元素逐一移动到que2。弹出
que1的最后一个元素并返回,就是栈顶元素。交换
que1和que2,确保que1始终用于添加元素,que2用于删除元素。时间复杂度:O(n),需要将
que1中除最后一个元素外的其他元素逐一移动到que2。空间复杂度:O(n),元素个数。
top 方法
返回
que1的最后一个元素,即栈顶元素。时间复杂度:O(1),只访问
que1的最后一个元素。空间复杂度:O(n),元素个数。
empty 方法
检查
que1是否为空,以判断栈是否为空。时间复杂度:O(1),只需要检查
que1是否为空。空间复杂度:O(n),元素个数。