class MyQueue {
Deque inStack;//存后入的元素,后入后出
Deque outStack;//为空时将in压入out
public MyQueue() {
inStack = new ArrayDeque<Integer>();
outStack = new ArrayDeque<Integer>();
}
public void push(int x) {
inStack.push(x);
}
public int pop() {
if (outStack.isEmpty()) {
in2out();
}
return outStack.pop();
}
public int peek() {
if (outStack.isEmpty()) {
in2out();
}
return outStack.peek();
}
public boolean empty() {
return inStack.isEmpty() && outStack.isEmpty();
}
private void in2out() {
while (!inStack.isEmpty()) {
outStack.push(inStack.pop());
}
}
}
/**
* Your MyQueue object will be instantiated and called as such:
* MyQueue obj = new MyQueue();
* obj.push(x);
* int param_2 = obj.pop();
* int param_3 = obj.peek();
* boolean param_4 = obj.empty();
*/
//双栈实现
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();
}
}
/**
* Your CQueue object will be instantiated and called as such:
* CQueue obj = new CQueue();
* obj.appendTail(value);
* int param_2 = obj.deleteHead();
*/
/**
实现思路:
用两个栈两次LIFO操作实现一个队列的FIFO
一个入栈,一个出栈,pop时,如果出栈为空,则从入栈转移元素,每个数至多入栈两次,出栈两次,均摊时间复杂度为O(1)
*/
class CQueue {
Deque<Integer> st1;
Deque<Integer> st2;
public CQueue() {
st1 = new ArrayDeque<>();
st2 = new ArrayDeque<>();
}
public void appendTail(int value) {
st1.add(value);
}
public int deleteHead() {
if (st2.isEmpty()){
while (!st1.isEmpty()){
st2.add(st1.pollLast());
}
}
if (st2.isEmpty()){
return -1;
}
return st2.pollLast();
}
}
/**
* 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();
*/
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 {
Stacks1;
Stacks2;
public CQueue() {
s1=new Stack<>();
s2=new Stack<>();
}
public void appendTail(int value) {
s2.push(value);
}
public int deleteHead() {
if(s1.isEmpty()&&s2.isEmpty())
return -1;
if(!s1.isEmpty())
return s1.pop();
else{
while(!s2.isEmpty()){
s1.push(s2.pop());
}
return s1.pop();
}
}
}
// 4
//3 2 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 in = new Stack<>(), out = new Stack<>();
public CQueue() {
}
public void appendTail(int value) {
in.push(value);
}
public int deleteHead() {
if(!out.isEmpty()){
return out.pop();
}
if(in.isEmpty()) return -1;
while(!in.isEmpty()){
out.push(in.pop());
}
return out.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<Integer> in;
Stack<Integer> out;
// 初始化
public CQueue() {
in = new Stack();
out = new Stack();
}
public void appendTail(int value) {
in.push(value);
}
public int deleteHead() {
// out里面有东西直接取
if (!out.isEmpty()) {
return out.pop();
}
// out里面取完了才能把in的导入out中
if (!in.isEmpty()) {
while (!in.isEmpty()) {
out.push(in.pop());
}
return out.pop();
}
return -1;
}
}
class CQueue {
Stack<Integer> s1;
Stack<Integer> s2;
public CQueue() {
s1 = new Stack<Integer>();
s2 = new Stack<Integer>();
}
public void appendTail(int value) {
while(!s2.empty()){
s1.push(s2.pop());
}
s1.push(value);
}
public int deleteHead() {
while(!s1.empty()){
s2.push(s1.pop());
}
if(!s2.empty())
return s2.pop();
else
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> in = new Stack<>();
Stack<Integer> out = new Stack<>();
// 构造函数初始化
public CQueue() {
in = new Stack<>();
out = new Stack<>();
}
// 入队
public void appendTail(int value) {
in.push(value);
}
// 出队
public int deleteHead() {
// 先检查out栈里有没有元素
if (!out.isEmpty()) {
return out.pop();
}
// 如果out栈没有元素,则把in栈中的元素 出栈 到out栈中, 然后再出栈
if (!in.isEmpty()) {
while (!in.isEmpty()) {
out.push(in.pop());
}
return out.pop();
}
// 如果in栈中都没有元素了,就直接返回-1
return -1;
}
}
class CQueue {
Stack<Integer> in;
Stack<Integer> out;
public CQueue() {
in = new Stack<>();
out = new Stack<>();
}
public void appendTail(int value) {
// 入队操作:in 操作元素入队
in.push(value);
}
public int deleteHead() {
// 将 in 中的元素逐一出栈,并添加到 out,此时 out 的栈顶元素就是队头元素
if (out.isEmpty()) {
while (!in.isEmpty()) {
out.push(in.pop());
}
}
return out.isEmpty() ? -1 : out.pop();
}
}
思路
基于两个栈(in 和 out)实现的队列,分别用于实现队列的入队和出队操作。
构造方法
初始化两个栈。
入队方法
appendTail(int value) 将元素 value 压入 in 栈。
时间复杂度:O(1),in 栈的入栈操作。
空间复杂度:O(n),两个栈
出队方法
deleteHead():out 栈的栈顶元素就是队列的头部元素。
如果 out 栈为空,将 in 栈中的所有元素逐一弹出并压入 out 栈,此时 out 栈的栈顶元素就是队列头部元素。
返回出队的元素:要么是 -1,要么是 out 的栈顶元素。
时间复杂度:O(n),将 in 栈中的所有元素逐一弹出并压入 out 栈。
空间复杂度:O(n),两个栈。
共以下 34 个回答
class MyQueue { inStack;//存后入的元素,后入后出 outStack;//为空时将in压入out
Deque
Deque
}
/**
* Your MyQueue object will be instantiated and called as such:
* MyQueue obj = new MyQueue();
* obj.push(x);
* int param_2 = obj.pop();
* int param_3 = obj.peek();
* boolean param_4 = obj.empty();
*/
###
有一个细节就是 pop阶段每次都是先判断s2是否为空
两个栈模拟队列
class CQueue {
public:
CQueue() {
private: m_sk1; m_sk2;
std::stack
std::stack
};
/**
* Your CQueue object will be instantiated and called as such:
* CQueue* obj = new CQueue();
* obj->appendTail(value);
* int param_2 = obj->deleteHead();
*/
代码s1; s2;
class CQueue {
Stack
Stack
public CQueue() {
s1=new Stack<>();
s2=new Stack<>();
}
}
// 4
//3 2 1
/**
* Your CQueue object will be instantiated and called as such:
* CQueue obj = new CQueue();
* obj.appendTail(value);
* int param_2 = obj.deleteHead();
*/
“`cpp
//测试
type MyQueue struct {
slice []int // 切片实现队列
“`cpp
“`c++
class CQueue {
public:
CQueue() {
private: stack1; stack2;
stack
stack
};
“`
使用两个栈模拟队列
使用两个栈 模拟队列
将先进后出翻转一下
“`c++sk_in; sk_out;
class CQueue {
public:
stack
stack
CQueue() {
};
“`
基础题型,一个栈存输入,一个栈存输出。in栈每次接收还的书,out栈每次相当于把in栈逆序存了之后,就可以pop最早还进来的书了
妙啊~~~
两个栈模拟
stack1为空判断很重要
class MyQueue { st1; st2;
Stack
Stack
}
时间:O(1)
空间:O(n)
双栈 in = new Stack<>(), out = new Stack<>();
class CQueue {
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();
*/
//时间复杂度O(1),空间复杂度O(N)
class CQueue { inStack,outStack;
Stack
打卡
用两个stack两手倒
重点pop的时候 如果stack2空 需要将stack1里所有的元素都pop并push进stack2 来保证顺序的正确性。
time O(n) space O(n)
思路:共两个栈,一个用做输入,一个用于输出。对于新到来的数据,全部放入输入栈内;需要输出时,如果输出栈不空,则输出栈栈顶即为结果,如果输出栈为空,则将输入栈的内容逐一弹出并压入输出栈内,此时输出栈栈顶即为结果。
时间复杂度O(N),空间复杂度O(N)。
时间复杂度O(n)
空间复杂度O(n)
时间复杂度:o(n) 空间复杂度:o(n)
入队操作时间复杂度为O(1)
出队操作最坏情况下O(N) 平均复杂度其实是O(1)
空间复杂度O(N)
1.思路分析
a.创建两个栈,一个用于作为输入函数的使用对象,一个作为输出函数的使用对象。
b.如果需要输出,则将输入栈的元素全部放置到输出栈中,由于栈起到逆序打印的作用,如果逆序两次,则会变成队列的正序。
2.代码
3.复杂度分析
思路
基于两个栈(
in和out)实现的队列,分别用于实现队列的入队和出队操作。构造方法
初始化两个栈。
入队方法
appendTail(int value)将元素value压入in栈。时间复杂度:O(1),
in栈的入栈操作。空间复杂度:O(n),两个栈
出队方法
deleteHead():out栈的栈顶元素就是队列的头部元素。如果
out栈为空,将in栈中的所有元素逐一弹出并压入out栈,此时out栈的栈顶元素就是队列头部元素。返回出队的元素:要么是
-1,要么是out的栈顶元素。时间复杂度:O(n),将
in栈中的所有元素逐一弹出并压入out栈。空间复杂度:O(n),两个栈。