class CQueue {
private Stack s1, s2;
public CQueue() {
s1 = new Stack();
s2 = new Stack();
}
public void appendTail(int value) {
s1.push(value);
}
//每次删除队列头结点都反复移动两个栈中的元素,实现删除栈的底部元素,模仿队列
public int deleteHead() {
if(s1.empty()) return -1;
while(!s1.empty()){
s2.push(s1.pop());
}
int v = (int)s2.pop();
while(!s2.empty()){
s1.push(s2.pop());
}
return v;
}
}
/**
* Your CQueue object will be instantiated and called as such:
* CQueue obj = new CQueue();
* obj.appendTail(value);
* int param_2 = obj.deleteHead();
*/
class CQueue {
Deque<Integer> inStack;
Deque<Integer> outStack;
public CQueue() {
inStack = new ArrayDeque<>();
outStack = new ArrayDeque<>();
}
public void appendTail(int value) {
inStack.push(value);
}
public int deleteHead() {
if (outStack.isEmpty()) {
while (!inStack.isEmpty()) {
outStack.push(inStack.pop());
}
}
if (outStack.isEmpty()) {
return -1;
}
return outStack.pop();
}
}
class CQueue {
Stack<Integer> A, B;
public CQueue() {
A = new Stack<Integer>();
B = new Stack<Integer>();
}
public void appendTail(int value) {
A.push(value);
}
public int deleteHead() {
if (!B.isEmpty()) {
return B.pop();
}
if (A.isEmpty()) {
return -1;
}
while (!A.isEmpty()) {
B.push(A.pop());
}
return B.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 {
public stackA: number[]
public stackB: number[]
constructor() {
this.stackA = [];
this.stackB = [];
}
appendTail(value: number): void {
this.stackA.push(value);
}
deleteHead(): number {
if (this.stackB.length) {
return this.stackB.pop();
} else {
while (this.stackA.length) {
this.stackB.push(this.stackA.pop());
}
}
return this.stackB.length ? this.stackB.pop() : -1;
}
}
/**
* Your CQueue object will be instantiated and called as such:
* var obj = new CQueue()
* obj.appendTail(value)
* var param_2 = obj.deleteHead()
*/
class CQueue {
//["CQueue","appendTail","deleteHead","deleteHead","deleteHead"]
//[[],[3],[],[],[]]
//输出:[null,null,3,-1,-1]
Stack<Integer> in;
Stack<Integer> out;
public CQueue() {
in = new Stack<>();
out = new Stack<>();
}
public void appendTail(int value) {
in.push(value);
}
public int deleteHead() {
if (!out.isEmpty()) {
return out.pop();
} else if (out.isEmpty()&&!in.isEmpty()) {
while (!in.isEmpty()) {
out.push(in.pop());
}
return out.pop();
} else {
return -1;
}
}
}
public class CQueue {
LinkedList<Integer> A;
LinkedList<Integer> B;
public CQueue() {
A = new LinkedList<Integer>();
B = new LinkedList<Integer>();
}
public void appendTail(int value) {
A.addLast(value);
}
public int deleteHead() {
if (!B.isEmpty()){
return B.removeLast();
}
if (A.isEmpty()){
return -1;
}
while(!A.isEmpty()){
B.addLast(A.removeLast());
}
return B.removeLast();
}
}
class CQueue {
LinkedList<Integer> A, B;
public CQueue() {
A = new LinkedList<Integer>();
B = new LinkedList<Integer>();
}
public void appendTail(int value) {
A.addLast(value);
}
public int deleteHead() {
if(!B.isEmpty()) return B.removeLast();
if(A.isEmpty()) return -1;
while(!A.isEmpty())
B.addLast(A.removeLast());
return B.removeLast();
}
}
class CQueue {
public Stack<Integer> st1;
public Stack<Integer> st2;
public CQueue() {
st1 = new Stack<>();
st2 = new Stack<>();
}
public void appendTail(int value) {
st1.push(value);
}
public int deleteHead() {
int ans = -1;
while(!st1.empty())
{
int tmp = st1.peek();
st2.push(tmp);
st1.pop();
}
//全部弹出,逆序
if(!st2.empty()){
ans = st2.peek();
st2.pop();
}
while(!st2.empty())
{
int tmp = st2.peek();
st2.pop();
st1.push(tmp);
}
return ans;
}
}
/**
* 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()) {
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 {
// A 作为 添加队尾元素的队列, B 作为 将元素倒序的队列
Deque<Integer> A,B;
public CQueue() {
A = new ArrayDeque<>();
B = new ArrayDeque<>();
}
public void appendTail(int value) {
A.addLast(value);
}
public int deleteHead() {
if(!B.isEmpty()) {
return B.removeLast();
}
if(A.isEmpty()) {
return -1;
}
while(!A.isEmpty()) {
B.addLast(A.removeLast());
}
return B.removeLast();
}
}
<pre><code class="language-java line-numbers">class CQueue {
Stack<Integer> stack1 = new Stack<>();
Stack<Integer> stack2 = new Stack<>();
public CQueue() {
}
public void appendTail(int value) {
stack1.push(value);
}
public int deleteHead() {
while(!stack1.isEmpty()){
stack2.push(stack1.pop());
}
int res = stack2.isEmpty()?-1:stack2.pop();
while(!stack2.isEmpty()){
stack1.push(stack2.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();
*/
</code></pre>
时间复杂度:O(n)
空间复杂度:O(n)
共以下 48 个回答
打
用两个栈实现队列 C++
delete: 使用辅助栈,当需要删除队首元素时,将所有元素依次出栈存入辅助栈,辅助栈中的元素就是队列的倒序,pop出栈顶就删除了队列第一个元素。时间复杂度O(n),空间复杂度O(n)
append: 直接入栈。时间复杂度O(1)
c只能用数组当做栈来操作,出队比较繁琐,注意函数的返回值类型是否匹配。
时间复杂度o1 空间复杂度为on
// 时间复杂度o1 空间复杂度为on
class CQueue {
}
直接实现即可
// C++
class CQueue {
public:
CQueue() {
private: stack1; stack2;
stack
stack
};
时间复杂度:O(n)
空间复杂度:O(n)
//两个栈 一个只进 一个只出 当出栈空了,入栈全部都进入出栈 in ; out ; (); ();
Stack
Stack
public CQueue() {
in =new Stack
out =new Stack
}
}
用两个栈实现队列
一个输入栈一个输出栈。
打卡
时间复杂度:O(n)
空间复杂度:O(n)
估计也就我用TS写了
代码如下
“`c++ s1; s2;
class CQueue {
public:
stack
stack
CQueue() {
s1.push(value);
}
int t=-1;
if(!s2.empty())
{
t=s2.top();
s2.pop();
return t;
}
if(!s1.empty())
{
while(!s1.empty())
{
s2.push(s1.top());
s1.pop();
}
t=s2.top();
s2.pop();
return t;
}
return -1;
}
};
//空间复杂度:O(n)
//时间复杂度:O(n)
c++ 代码 inStack, outStack;
class CQueue {
private:
stack
public:
CQueue() {}
};
两个栈实现队列
第一个栈负责入
第二个栈负责出 实现队列操作
代码
用两个栈来实现队列
第四天 instk, outstk;
class CQueue {
private:
stack
public:
CQueue() {
};
Day4:下面是代码
插入时间复杂度O1
时间复杂度O(N)
空间复杂度O(N)
“`c++
class CQueue {
public:
CQueue() {
private: left; right;
std::stack
std::stack
};
“`
空间复杂度:O(n)
时间复杂度:O(1)
两个栈实现队列
”’java stack1; stack2;
class CQueue {
LinkedList
LinkedList
public CQueue() {
stack1 = new LinkedList<>();
stack2 = new LinkedList<>();
}
}
”’
剑指 Offer 09. 用两个栈实现队列
四 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();
*/
先来点简单题
append 操作:O(1)
deleteHead操作: O(n)
出队时 若出队栈不为空 直接弹出 否则将入队栈压入出队栈
时间复杂度:O(n)
空间复杂度:O(n)