}
void appendTail(int value) {
in.push(value);
}
int deleteHead() {
if(out.empty())
{
while(!in.empty())
{
out.push(in.top());
in.pop();
}
}
if (out.empty())
{
return -1;
}
else
{
int res = out.top();
out.pop();
return res;}
}
};
/**
* Your CQueue object will be instantiated and called as such:
* CQueue* obj = new CQueue();
* obj->appendTail(value);
* int param_2 = obj->deleteHead();
*/
时间复杂度O(n),空间复杂度O(1)
class CQueue {
public:
CQueue() {
}
void appendTail(int value) {
stack1.push(value);
}
int deleteHead() {
if(!stack2.empty())
{
int res = stack2.top();
stack2.pop();
return res;
}
if(!stack1.empty())
{
while(!stack1.empty())
{
stack2.push(stack1.top());
stack1.pop();
}
int res = stack2.top();
stack2.pop();
return res;
}
return -1;
}
private:
stack stack1;
stack stack2;
};
/**
* Your CQueue object will be instantiated and called as such:
* CQueue* obj = new CQueue();
* obj->appendTail(value);
* int param_2 = obj->deleteHead();
*/
“`C++
class CQueue {
public:
stack< int > stack1;
stack< int > stack2;
CQueue() {
// stack1 = new stack();
// stack2 = new stack();
}
void appendTail(int value) {
stack1.push(value);
}
int deleteHead() {
if (!stack2.empty()) {
int top = stack2.top();
stack2.pop();
return top;
}
if (!stack1.empty()) {
while(!stack1.empty()) {
int top = stack1.top();
stack2.push(top);
stack1.pop();
}
int top = stack2.top();
stack2.pop();
return top;
}
return -1;
}
class CQueue {
Stack stackA, stackB;
public CQueue() {
stackA = new Stack<>();
stackB = new Stack<>();
}
public void appendTail(int value) {
stackA.add(value);
}
public int deleteHead() {
if (!stackB.isEmpty()) {
return stackB.pop();
}
if (!stackA.isEmpty()) {
while (!stackA.isEmpty()) {
stackB.add(stackA.pop());
}
return stackB.pop();
}
return -1;
}
}
/**
* Your CQueue object will be instantiated and called as such:
* CQueue obj = new CQueue();
* obj.appendTail(value);
* int param_2 = obj.deleteHead();
*/
/**
* Your CQueue object will be instantiated and called as such:
* CQueue* obj = new CQueue();
* obj->appendTail(value);
* int param_2 = obj->deleteHead();
*/
时间复杂度O(n)
class CQueue {
Stack<Integer> stack1;
Stack<Integer> stack2;
public CQueue() {
stack1 = new Stack<>();
stack2 = new Stack<>();
}
public void appendTail(int value) {
stack1.push(value);
}
public int deleteHead() {
if (!stack2.isEmpty()) {
return stack2.pop();
}
if (!stack1.isEmpty()) {
while (!stack1.isEmpty()) {
stack2.push(stack1.pop());
}
return stack2.pop();
}
return -1;
}
}
/**
* Your CQueue object will be instantiated and called as such:
* CQueue obj = new CQueue();
* obj.appendTail(value);
* int param_2 = obj.deleteHead();
*/
public CQueue() {
st1 = new ArrayDeque<>();
st2 = new ArrayDeque<>();
}
public void appendTail(int value) {
st1.addLast(value);
}
public int deleteHead() {
if(st1.isEmpty() && st2.isEmpty()) return -1;
if(st2.isEmpty()){
while(!st1.isEmpty()){
st2.addLast(st1.removeLast());
}
}
return st2.removeLast();
}
}
/**
* Your CQueue object will be instantiated and called as such:
* CQueue obj = new CQueue();
* obj.appendTail(value);
* int param_2 = obj.deleteHead();
*/
class CQueue {
Stack s1 ;
Stack s2 ;
public CQueue() {
s1 = new Stack<>();
s2 = new Stack<>();
}
public void appendTail(int value) {
s1.push(value);
}
public int deleteHead() {
if (!s2.isEmpty())
{
return s2.pop();
}else{
while (!s1.isEmpty())
{
s2.push(s1.pop());
}
return s2.isEmpty()?-1:s2.pop();
}
}
}
/**
* Your CQueue object will be instantiated and called as such:
* CQueue obj = new CQueue();
* obj.appendTail(value);
* int param_2 = obj.deleteHead();
*/打卡
栈实现队列
class CQueue {
Stack stack1;
Stack stack2;
public CQueue() {
stack1 = new Stack<>();
stack2 = new Stack<>();
}
public void appendTail(int value) {
stack1.push(value);
}
public int deleteHead() {
if(stack2.isEmpty()){
while(!stack1.isEmpty()){
stack2.push(stack1.pop());
}
}
return stack2.isEmpty() ? -1 : stack2.pop();
}
}
/**
* Your CQueue object will be instantiated and called as such:
* CQueue obj = new CQueue();
* obj.appendTail(value);
* int param_2 = obj.deleteHead();
*/
class CQueue {
LinkedList A,B;
public CQueue() {
//两个链表,类似两个栈
A = new LinkedList();
B = new LinkedList();
}
//给栈A压入元素
public void appendTail(int value) {
A.addLast(value);
}
//删除头结点元素
public int deleteHead() {
if(!B.isEmpty()) return B.removeLast();//判断B是否为空,不为空删除最末尾的结点
if(A.isEmpty()) return -1;//如果A为空,说明没有内容
//如果A不为空,将栈A的元素逆序,调入栈B
while(!A.isEmpty())
B.addLast(A.removeLast());
return B.removeLast();
}
}
/**
* Your CQueue object will be instantiated and called as such:
* CQueue obj = new CQueue();
* obj.appendTail(value);
* int param_2 = obj.deleteHead();
*/
刚开始题目没看懂,后面看了精选回答才理解,然后java有许多自带的可以用的方法不太熟练掌握
时间复杂度O(1)
空间复杂度O(n)
class CQueue {
public:
CQueue()
{}
void appendTail(int value)
{
main.push(value);
}
int deleteHead()
{
int n = main.size();
if(n == 0)
return -1;
for(int i = 0; i < n - 1; i++)
{
tmp.push(main.top());
main.pop();
}
int res = main.top();
main.pop();
while(!tmp.empty())
{
main.push(tmp.top());
tmp.pop();
}
return res;
}
private:
stack<int> main;
stack<int> tmp;
};
/**
* Your CQueue object will be instantiated and called as such:
* CQueue* obj = new CQueue();
* obj->appendTail(value);
* int param_2 = obj->deleteHead();
*/
//本质上是用两个栈实现一个队列,A栈全部推入B栈就是栈的倒序
class CQueue {
LinkedList<Integer> stack1;
LinkedList<Integer> stack2;
public CQueue() {
this.stack1 = new LinkedList<Integer>();
this.stack2 = new LinkedList<Integer>();
}
public void appendTail(int value) {
stack1.addLast(value);
}
public int deleteHead() {
if(!stack2.isEmpty()) return stack2.removeLast();
if(stack1.isEmpty()) return -1;
while(!stack1.isEmpty()) {
stack2.addLast(stack1.removeLast());
}
return stack2.removeLast();
}
}
/**
* Your CQueue object will be instantiated and called as such:
* CQueue obj = new CQueue();
* obj.appendTail(value);
* int param_2 = obj.deleteHead();
*/
class CQueue {
Stack<Integer> stack1;
Stack<Integer> stack2;
public CQueue() {
stack1 = new Stack<>();
stack2 = new Stack<>();
}
public void appendTail(int value) {
stack1.push(value);
}
public int deleteHead() {
if(!stack2.isEmpty()){
return stack2.pop();
}
if(!stack1.isEmpty()){
while(!stack1.isEmpty()){
stack2.push(stack1.pop());
}
return stack2.pop();
}
return -1;
}
}
/**
* Your CQueue object will be instantiated and called as such:
* CQueue obj = new CQueue();
* obj.appendTail(value);
* int param_2 = obj.deleteHead();
*/
class CQueue {
Stack<Integer> stack1;
Stack<Integer> stack2;
public CQueue() {
stack1 = new Stack<>();
stack2 = new Stack<>();
}
public void appendTail(int value) {
stack1.push(value);
}
public int deleteHead() {
if(!stack2.isEmpty()){
return stack2.pop();
}
if(!stack1.isEmpty()){
while(!stack1.isEmpty()){
stack2.push(stack1.pop());
}
return stack2.pop();
}
return -1;
}
}
/**
* Your CQueue object will be instantiated and called as such:
* CQueue obj = new CQueue();
* obj.appendTail(value);
* int param_2 = obj.deleteHead();
*/
共以下 53 个回答
class CQueue:
时间复杂度: O(1),push 为 O(1),pop 为均摊 O(1)。对于每个元素,至多入栈和出栈各两次(Stack_In和Stack_Out至多各进出栈一次),故均摊复杂度为 O(1)。
空间复杂度:O(n)。其中 n 是操作总数。对于有 n 次 push 操作的情况,队列中会有 n 个元素,故空间复杂度为 O(n)。
//用两个栈实现一个队列,in栈负责队列的push,out栈负责队列的pop(),关键在于两个点(1)判断Out栈是否为空if,若为空移动in中所有元素至out(2)In栈中的元素必须作为一个整体一起移动至out栈while,这两个关键点理解,还需要加强记忆 in; out;
class CQueue {
public:
stack
stack
CQueue() {
};
/**
* Your CQueue object will be instantiated and called as such:
* CQueue* obj = new CQueue();
* obj->appendTail(value);
* int param_2 = obj->deleteHead();
*/
时间复杂度O(n) 空间复杂度O(1)
def init(self):
self.stack1 = []
self.stack2 = []
时间复杂度O(n),空间复杂度O(1)
class CQueue {
public:
CQueue() {
private: stack1; stack2;
stack
stack
};
/**
* Your CQueue object will be instantiated and called as such:
* CQueue* obj = new CQueue();
* obj->appendTail(value);
* int param_2 = obj->deleteHead();
*/
时间复杂度:O(n)
空间复杂度:O(1)
思路:直接使用队列
push方法时间复杂度O(1),空间复杂度O(1)st1; st2;
pop()方法时间复杂度O(N),空间复杂度O(1)
class CQueue {
public:
stack
stack
CQueue() {
};
“`C++(); ();
class CQueue {
public:
stack< int > stack1;
stack< int > stack2;
CQueue() {
// stack1 = new stack
// stack2 = new stack
}
};
“`C++
使用两个堆栈实现队列
class CQueue { qIn; qOut;
public:
stack
stack
CQueue() {
”’java stack1; stack2;
class CQueue {
public:
stack
stack
CQueue() {
};
”’
时间复杂度O(n) 空间复杂度O(n)
class CQueue { stackin; stackout;
Stack
Stack
}
时间复杂度O(n)空间复杂度O(n)
class CQueue { A; B; (); ();
Stack
Stack
public CQueue() {
A = new Stack
B = new Stack
}
}时间复杂度O(n)空间复杂度O(n)
class CQueue:
思路:两个列表,分别表示输入栈,和输出栈
输入栈用来还书
输出栈用来借书
借书时先看看输出栈还有书在没,如果没有将输入栈内的书全部放在输出栈内,后还的书先放进输出栈内,整体实现先还先出
时间复杂度:o(n)
空间复杂度:o(n)
思路:借还规律符合队列,两个书架符合栈的特征,所以要用两个栈来模拟,实现队列 in; out;
class CQueue {
public:
stack
stack
CQueue() {
}
};
时间复杂度O(n)
空间复杂度O(n)
修改为两个栈,栈A做录入,栈B做导出;
因为栈是先进后出,做两次栈保存操作后,最先进的值在第二个栈肯定先出
所以栈B中如果有值的话,直接取值就是最新录入的值。 如果栈B为空,则从栈A中做次同步值。
如果栈A、栈B都空情况,此时返回-1
class CQueue { stackA, stackB;
Stack
public CQueue() {
stackA = new Stack<>();
stackB = new Stack<>();
}
}
/**
* Your CQueue object will be instantiated and called as such:
* CQueue obj = new CQueue();
* obj.appendTail(value);
* int param_2 = obj.deleteHead();
*/
typedef struct {
// 参数类似形参,只能定义,不能赋值
int stack1[500];
int top1;
int stack2[500];
int top2;
} CQueue;
CQueue* cQueueCreate() {
CQueue *obj = malloc(sizeof(CQueue));
obj->top1 = -1;
obj->top2 = -1;
return obj;
}
void cQueueAppendTail(CQueue* obj, int value) {
obj->stack1[++obj->top1] = value;
}
int cQueueDeleteHead(CQueue* obj) {
if (obj->top2 -1 && obj->top1 -1) {
return -1;
} else if (obj->top2 -1) {
while (obj->top1 > 0) { // 留stack1中最后一个元素,直接出栈,不用入栈stack2后再出栈
obj->stack2[++obj->top2] = obj->stack1[obj->top1–];
}
return obj->stack1[obj->top1–];
} else {
return obj->stack2[obj->top2–];
}
}
void cQueueFree(CQueue* obj) {
free(obj);
}打卡
class CQueue {
public:
CQueue() {
private: inStack, outStack;
stack
void in2out()
{
while(!inStack.empty())
{
outStack.push(inStack.top());
inStack.pop();
}
}
};
入队的时间复杂度是O(1);出队的时间复杂度是O(n);空间复杂度是O(n)
javaA,B; (); ();
class CQueue {
LinkedList
public CQueue() {
A = new LinkedList
B = new LinkedList
}
}
提交
class CQueue { inStack,outStack;
private:
stack
public:
CQueue(){
}
};
打卡
appendTail时间复杂度O(1),deleteHead时间复杂度O(n),空间复杂度O(n)
class CQueue {
}
利用两个栈实现FIFO
时间复杂度O(n) 额外空间复杂度O(n)
解题思路:
使用两个栈stackin and stackout
in 栈负责接收数据
out栈负责输出数据
当数据输入的时候要保证数据全部在in栈里
使用while(!stackout.empty())将out栈全部转移到in栈中输入
当数据输出的时候要保证数据全部在out栈里
使用while(!stackin.empty()) 将in栈全部转移到out栈中输出
class CQueue { s1; s2;
public:
stack
stack
CQueue() {
};
/**
* Your CQueue object will be instantiated and called as such:
* CQueue* obj = new CQueue();
* obj->appendTail(value);
* int param_2 = obj->deleteHead();
*/
时间复杂度O(n)
用两个栈实现队列的入队出队操作
class CQueue {
}
时间复杂度:appendTail 方法为 O(1),deleteHead 方法在最坏情况下为 O(n)
空间复杂度:O(n)
class CQueue { st1; st2;
Deque
Deque
}
/**
* Your CQueue object will be instantiated and called as such:
* CQueue obj = new CQueue();
* obj.appendTail(value);
* int param_2 = obj.deleteHead();
*/
时间复杂度O(1),空间复杂度O(n)
class CQueue { s1 ; s2 ;
Stack
Stack
public CQueue() {
s1 = new Stack<>();
s2 = new Stack<>();
}
/**
* Your CQueue object will be instantiated and called as such:
* CQueue obj = new CQueue();
* obj.appendTail(value);
* int param_2 = obj.deleteHead();
*/打卡
栈实现队列 stack1; stack2;
class CQueue {
Stack
Stack
public CQueue() {
stack1 = new Stack<>();
stack2 = new Stack<>();
}
}
/**
* Your CQueue object will be instantiated and called as such:
* CQueue obj = new CQueue();
* obj.appendTail(value);
* int param_2 = obj.deleteHead();
*/
思路:定义两个栈sin, sout; 插入时直接使用
sin.push(x), 输出时判断:如果sin和sout都空,说明没有元素,返回-1;如果sout不空,则直接输出sout.top(), 如果sout空, 则将sin的值插入到sout,然后输出sout栈顶元素。“`c++ sIn, sOut;
class CQueue {
private:
stack
public:
CQueue() {
};
“`
class CQueue { A,B; (); ();
LinkedList
public CQueue() {
//两个链表,类似两个栈
A = new LinkedList
B = new LinkedList
}
/**
* Your CQueue object will be instantiated and called as such:
* CQueue obj = new CQueue();
* obj.appendTail(value);
* int param_2 = obj.deleteHead();
*/
刚开始题目没看懂,后面看了精选回答才理解,然后java有许多自带的可以用的方法不太熟练掌握
时间复杂度O(1)
空间复杂度O(n)
public CQueue() {(); ();
A =new LinkedList
B =new LinkedList
}
思路:一个做入栈队 ,一个做出栈队
打卡
https://leetcode.cn/problems/implement-queue-using-stacks/solutions/2668124/zhan-mo-ni-dui-lie-by-yukislab-31e6
时空复杂度O(n)
class CQueue { stack1; stack2;
Stack
Stack
public CQueue() {
stack1=new Stack<>();
stack2=new Stack<>();
}
}
利用两个栈的先进后出,完成队列的先进先出功能
day4第一题
出队列时时间复杂度$O(n)$
时间复杂度:O(n)
空间复杂度:O(1)
时间复杂度O(2n)
空间复杂度O(n)
##C++
时间:O(n)空间O(1)
两个栈实现队列java
时间复杂度O(n),空间复杂度O(1)
两个栈来进行操作,一个栈添加,一个栈删除,只需要把栈的数据转移一次就变成和队列一样顺序的了。时间复杂度O(n),空间复杂度O(n)
class CQueue:
Your CQueue object will be instantiated and called as such:
obj = CQueue()
obj.appendTail(value)
param_2 = obj.deleteHead()
时间o(n) 空间o(1)
class CQueue { borrowCar; returnCar;
Stack
Stack
}
//时间复杂度O(n)
//空间复杂度O(1)