public MyStack() {
queue = new LinkedList<>();
}
public void push(int x) {
int size = queue.size();
queue.offer(x);
for (int i = 0; i < size; i++) {
queue.offer(queue.poll());
}
}
public int pop() {
return queue.poll();
}
public int top() {
return queue.peek();
}
public boolean empty() {
return queue.isEmpty();
}
“`java
class MyStack {
Queue q1;
Queue q2;
public MyStack() {
q1 = new LinkedList<>();
q2 = new LinkedList<>();
}
public void push(int x) {
q2.offer(x);
while(!q1.isEmpty()){
q2.offer(q1.poll());
}
Queue<Integer> q3 = q1;
q1 = q2;
q2 = q3;
}
public int pop() {
return q1.poll();
}
public int top() {
return q1.peek();
}
public boolean empty() {
return q1.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> q;
int top;
public MyStack() {
q = new LinkedList();
top = -1;
}
public void push(int x) {
q.offer(x);
top = x;
}
public int pop() {
int size = q.size();
while(size > 2){
q.offer(q.poll());
size--;
}
top = q.peek();
q.offer(q.poll());
return q.poll();
}
public int top() {
return top;
}
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();
*/
class MyStack {
private Queue<Integer> inQueue;
private Queue<Integer> outQueue;
public MyStack() {
inQueue = new LinkedList<>();
outQueue = new LinkedList<>();
}
public void push(int x) {
while (!inQueue.isEmpty()) {
outQueue.offer(inQueue.poll());
}
inQueue.offer(x);
while (!outQueue.isEmpty()) {
inQueue.offer(outQueue.poll());
}
}
public int pop() {
return inQueue.poll();
}
public int top() {
return inQueue.peek();
}
public boolean empty() {
return inQueue.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();
*/
java
class MyStack {
private Queue<Integer> a;//輸入隊列
private Queue<Integer> b;//輸出隊列
public MyStack() {
a = new LinkedList<>();
b = new LinkedList<>();
}
public void push(int x) {
a.offer(x);
while(!b.isEmpty()) {
a.offer(b.poll()); //使最开始进入的加入a的后面,实现后进先出
}
Queue tmp = a; //交換隊列,使得a为空
a = b;
b = tmp;
}
public int pop() {
return b.poll();
}
public int top() {
return b.peek();
}
public boolean empty() {
return b.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();
*/
void appendTail(int value) {
st1.push(value);
}
int deleteHead() {
int value=-1;
if(!st2.empty()){
value = st2.top();
st2.pop();
return value;
}
if(st1.empty())return value;
while(!st1.empty()){
st2.push(st1.top());
st1.pop();
}
value = st2.top();
st2.pop();
return value;
}
};
/**
* Your CQueue object will be instantiated and called as such:
* CQueue* obj = new CQueue();
* obj->appendTail(value);
* int param_2 = obj->deleteHead();
*/
“`
“`c++
class MyStack {
queueque;
public:
MyStack() {
}
void push(int x) {
que.push(x);
int sz = que.size();
for (int i = 0; i < sz - 1; i++) {
int curr = que.front();
que.pop();
que.push(curr);
}
}
int pop() {
int curr = que.front();
que.pop();
return curr;
}
int top() {
return que.front();
}
bool empty() {
return que.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 {
private Deque<Integer> queue;
public MyStack() {
queue = new ArrayDeque<>();
}
public void push(int x) {
int count = queue.size();
queue.offer(x);
for (int i = 0; i < count; i++) {
queue.offer(queue.poll());
}
}
public int pop() {
return queue.poll();
}
public int top() {
return queue.peek();
}
public boolean empty() {
return queue.isEmpty();
}
}
Queue<Integer> queue1;
Queue<Integer> queue2;
public MyStack() {
this.queue1 = new LinkedList<>();
this.queue2 = new LinkedList<>();
}
public void push(int x) {
if (queue1.isEmpty()) {
queue1.add(x);
while (!queue2.isEmpty()) {
queue1.addAll(queue2);
queue2.clear();
}
} else {
queue2.add(x);
queue2.addAll(queue1);
queue1.clear();
}
}
public int pop() {
return queue1.isEmpty() ? queue2.poll() : queue1.poll();
}
public int top() {
return queue1.isEmpty() ? queue2.peek() : queue1.peek();
}
public boolean empty() {
return queue1.isEmpty() && queue2.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> q;
public MyStack() {
q = new LinkedList<Integer>();
}
public void push(int x) {
q.offer(x);
int size = q.size();
//将x放入第一位
while(size-- > 1) {
q.offer(q.poll());
}
}
public int pop() {
return q.poll();
}
public int top() {
return q.peek();
}
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();
*/
class MyStack {
LinkedList queue;
public MyStack() {
queue=new LinkedList<>();
}
public void push(int x) {
queue.offer(x);
//把前面都循环到队尾(从对头拿到队尾)
for(int i=0;i<queue.size()-1;i++){
queue.offer(queue.poll());
}
}
public int pop() {
return queue.poll();
}
public int top() {
return queue.peek();
}
public boolean empty() {
if(!queue.isEmpty()){
return false;
}
return true;
}
}
/**
* 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();
*/
共以下 52 个回答
题目不难,考的是数据结构的掌握
“`java queue;
class MyStack {
private Queue
}
“`java q1; q2;
class MyStack {
Queue
Queue
public MyStack() {
q1 = new LinkedList<>();
q2 = new LinkedList<>();
}
}
/**
* 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 {
}
代码
“`javapush_back(x);
使用两个队列,其中一个进行辅助
时间复杂度 插入O(n),删除O(1)
空间复杂度 O(n)
class MyList{
int data[100];
int ptr = 0;
int front = 0 ;
public:
MyList(){}
void push_back(int val){
data[ptr++] = val ;
}
int peek(){
if(ptr == front) return-1;
return data[front];
}
void pop_front(){ if(front
while(!primary->empty()){
secondary->push_back(primary->peek());
primary->pop_front();
}
std: : swap(primary, secondary);
}
int pop(){
int val = primary ->peek();}
primary ->pop_front();
return val;
}
int pop() { return primary ->peek():}
bool empty() { return primary ->empty(); }
};
使用两个队列,其中一个进行辅助
【时间复杂度】插入O(n),删除O(1)
【空间复杂度】O(n)
时间复杂度为O(1)
空间复杂度为O(n)
代码:
这个实现中,我们使用两个队列 queue1 和 queue2 来模拟栈的行为。其中一个队列始终保持为空,另一个队列用于存储栈中的元素。在 push 操作中,我们将元素压入非空队列。在 pop 操作中,我们将非空队列中的元素依次转移到另一个队列,直到剩下最后一个元素,然后返回该元素。 top 操作返回非空队列中的最后一个元素。 empty 操作判断两个队列是否都为空。
将队2作为储存栈的容器,队1作为入栈的辅助容器,当进行入栈时,先将要入栈的元素入队,然后将队2元素对队1入队,此时刚入栈的元素就在队首,再将队1元素全部入队队2,此时完成入栈操作,出栈以及返回顶端,都对队2进行出队等操作即可
时间复杂度:
入栈:O(n)
出栈、获取栈顶元素、判断是否为空:O(1)
空间复杂度:O(n)
时间复杂度O(N), 空间复杂度O(1)
主队列辅助队列
c++ 实现
两个队列实现栈
实现
时间复杂度:O(1)
“`C++ st1; st2;
//q1,q2是两个queue,然后每次top和pop就把一个队列力的全部元素转移到另一个里面。但是觉得时间每次top 和pop都是O(n)的,比较慢
class CQueue {
private:
stack
stack
public:
CQueue() {
}
};
/**
* Your CQueue object will be instantiated and called as such:
* CQueue* obj = new CQueue();
* obj->appendTail(value);
* int param_2 = obj->deleteHead();
*/
“`
JAVA用队列实现栈 queue1; queue2;
class MyStack {
private Queue
private Queue
public MyStack() {
queue1 = new LinkedList<>();
queue2 = new LinkedList<>();
}
}
时间复杂度:o(1)
实现
C++实现
”’java实现 myQueue = new LinkedList();
class MyStack {
Queue
public MyStack() {
}
}
C++
永远保证最新入栈的在队列的最前端
TC: O(n)
SC: O(n)
“`c++que;
class MyStack {
queue
public:
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();
*/
“`
注意import deque
需要注意的是队列只能移除队首的元素,但是可以获取队尾的元素
为了保证每次都能进行如此的转置,所以需要一个固定的空链表,因此利用temp将queue1和queue2进行互换。互换成功之后可以直接使用remove、peek、进行栈相关数据的实现。
(小声bibi,我写的思路估计也就自己能看懂,胡言乱语中)
将队列头部的元素(除最后一个元素外),重新添加到队列尾部,此时再去弹出元素就是栈的顺序。
c++解题思路 q1; q2; qt1; qt2;
class MyStack {
queue
queue
public:
MyStack() {
queue
q1 = qt1;
queue
q2 = qt2;
}
};
思路:使用两个队列,一个队列代表是真正的栈,另外一个是辅助栈,即将入栈的元素先存放在辅助栈中,然后将真正栈中队列的元素入辅助栈,然后将元素交换至真正的栈。 queue1; // 主栈 queue2; // 辅助栈
”’c++
class MyStack {
public:
queue
queue
MyStack() {
};
””
使用一个队列模拟栈,当push时,先计算队列元素个数count,然后将元素移入队列尾端,并且将队首元素移到队尾count次,这样push的元素到了队首,保证了后入先出原则。
时间:O(n)
空间:O(n)
时间复杂度:O(n)
额外空间复杂度:O(1)
单队列
双队列
【时间O(n),空间O(n)】
插入正常
弹出需要倒出全部元素,弹出队尾元素
1、自己想的方法
保持第一个队列存放所有元素,第二个队列为空
若要压栈,在有元素的队列里进队
若要出栈,把有元素的队列中除了最后一个元素之外的所有元素倒进另一个队列里面,交换队列
若要返回栈顶元素,把有元素的队列中的所有元素倒进另一个队列里面,此队清空,在倒最后一个元素的时候记录一下,这样就能返回栈顶了,交换队列
若两个队列都是空的,则返回 false
这样入栈简单,时间复杂度为O(1), 出栈和返回栈顶元素较难,时间复杂度为O(n)
2、看题解发现还有一种做法
一个队列按照队列前端是后入队数,队列后端是先入队数存储,一个队列为空
在获取栈顶元素和出栈时,操作第一个队列的前端即可
在入栈时,把新元素先放入第二个队列,然后,第一个队列的数依次出队,进入第二个队列,交换第一和第二个队列(这里可以用swap)
若第一个队列为空,则栈为空
这样入栈较难 O(n),出栈和返回栈顶元素较简单 O(1)
思路
代码
[TOC]
思路
解题方法
复杂度
Code
“`Java []
class 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();
* boolean param_4 = obj.empty();
*/
“`
时空复杂度:O(n)
1、创建两个队列s1、s2,队列由LinkedList实现
2、当模拟入栈时,判断s1是否为空,若不为空则循环将s1的数据poll出到s2中,再s1 offer新的数据,再将s2中的依次入队列s1
3、其他操作可用队列自带方法,poll出队列第一个元素并删除原队列中该元素,peek取出队尾第一个元素不删除原队列
C++实现
思路:创建两个队列q1和q2。保持队列1为栈顶位置所在队列,当有入栈操作,将元素入队q2,再将q1中元素,全部入队q2,此时再交换q1和q2即可。剩下的操作就简单了,pop和top操作只需要取得q1队头即可,empty操作也只需要对q1判空即可
这里是Ming的打卡:
利用一个队列模拟栈思路:每放入一个数就将这个数放入第一位,所以其他元素往后移动
不顺畅
class MyStack { queue;
LinkedList
public MyStack() {
queue=new LinkedList<>();
}
}
/**
* 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();
*/
c++实现que1; que2;
private:
queue
queue
public:
MyStack() {
“`c++ stIn; stOut;
class CQueue {
public:
stack
stack
CQueue() {
}
//在队列尾部插入整数
void appendTail(int value) {
stIn.push(value);
}
//在队列头部删除整数
int deleteHead()
{
if(stIn.empty() && stOut.empty())
{
return -1;
}
//stOut为空
if(stOut.empty())
{
while(!stIn.empty())
{
stOut.push(stIn.top());
stIn.pop();
}
}
int result = stOut.top();
stOut.pop();
return result;
}
};
java实现
idea中正常运行,不知道为什么LeetCode报错。
头部3
是否为空false
3
2
1
是否为空true