class CQueue {
Stack stack_in;
Stack stack_out;
public CQueue() {
stack_in = new Stack<>();
stack_out = new Stack<>();
}
public void appendTail(int value) {
stack_in.push(value);
}
public int deleteHead() {
if (stack_out.isEmpty()){
while (!stack_in.isEmpty()){
stack_out.push(stack_in.pop());
}
}
if (stack_out.isEmpty()){
return -1;
}
return stack_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 {
private Stack<Integer> s1,s2;
public CQueue() {
s1 = new Stack();
s2 = new Stack();
}
public void appendTail(int value) {
s1.add(value);
}
public int deleteHead() {
// 空队列
if(s1.isEmpty() && s2.isEmpty())
return -1;
if(!s2.isEmpty())
return s2.pop();
while(!s1.isEmpty()){
s2.push(s1.pop());
}
return 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 {
private Stack<Integer> inStack;
private Stack<Integer> outStack;
public CQueue() {
inStack = new Stack<>();
outStack = new Stack<>();
}
public void appendTail(int value) {
inStack.push(value);
}
public int deleteHead() {
if (!outStack.isEmpty()) {
return outStack.pop();
}
if (inStack.isEmpty()) {
return -1;
}
while (!inStack.isEmpty()) {
Integer value = inStack.pop();
outStack.push(value);
}
return outStack.pop();
}
}
/**
* Your CQueue object will be instantiated and called as such:
* CQueue obj = new CQueue();
* obj.appendTail(value);
* int param_2 = obj.deleteHead();
*/
java
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:
def __init__(self):
self.input, self.output = [], []
def appendTail(self, value: int) -> None:
self.input.append(value)
def deleteHead(self) -> int:
if self.output:
return self.output.pop()
if not self.input:
return -1
while self.input:
self.output.append(self.input.pop())
return self.output.pop()
# Your CQueue object will be instantiated and called as such:
# obj = CQueue()
# obj.appendTail(value)
# param_2 = obj.deleteHead()
class CQueue {
LinkedList<Integer> A, B;
// 让元素“入队”时,只要把元素压入 A 即可
// 让元素“出队”时
// 若 B 不为空,可以把 A 的所有元素取出再添加进 B,这时候 B 中元素就是先进先出顺序了
// 若 B 不为空,则直接返回 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();
}
}
“`c++
class CQueue {
stackstk1;
stackstk2;
public:
CQueue() {
}
void appendTail(int value) {
stk1.push(value);
}
int deleteHead() {
if (stk2.empty()) {
while (!stk1.empty()) {
stk2.push(stk1.top());
stk1.pop();
}
}
if (stk2.empty()) {
return -1;
} else {
int res = stk2.top();
stk2.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();
*/
class CQueue {
public:
stack<int> stk1;
stack<int> stk2;
CQueue() {
}
void appendTail(int value) {
stk1.push(value);
}
int deleteHead() {
if(stk1.empty() && stk2.empty())
return -1;
if(stk2.empty()){
while(!stk1.empty()){
int num = stk1.top();
stk1.pop();
stk2.push(num);
}
}
int ans = stk2.top();
stk2.pop();
return ans;
}
};
/**
* Your CQueue object will be instantiated and called as such:
* CQueue* obj = new CQueue();
* obj->appendTail(value);
* int param_2 = obj->deleteHead();
*/
//创建一个进栈,一个出栈,进栈只管操作进入的数字,当出的栈为空的时候,从进栈中拿数据,如果未拿到则为-1,负责就拿出数据进行pop操作
class CQueue {
Stack<Integer> appendStack;
Stack<Integer> deleteStack;
public CQueue() {
this.appendStack = new Stack<>();
this.deleteStack = new Stack<>();
}
public void appendTail(int value) {
appendStack.push(value);
}
public int deleteHead() {
if (deleteStack.isEmpty()){
while (!appendStack.isEmpty()){
deleteStack.push(appendStack.pop());
}
}
if (deleteStack.isEmpty()){
return -1;
}else {
return deleteStack.pop();
}
}
}
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 {
//用两个栈实现队列
//队尾 栈顶压入
ArrayDeque stackW;
ArrayDeque stackT;
public CQueue() {
stackW=new ArrayDeque<Integer>();
stackT=new ArrayDeque<Integer>();
}
public void appendTail(int value) {
stackW.push(value);
}
public int deleteHead() {
if(stackT.isEmpty()){
while(!stackW.isEmpty()){
stackT.push(stackW.pop());
}
}
if(stackT.isEmpty() && stackW.isEmpty()){
return -1;
}else{
return stackT.pop();
}
}
}
/**
* Your CQueue object will be instantiated and called as such:
* CQueue obj = new CQueue();
* obj.appendTail(value);
* int param_2 = obj.deleteHead();
*/
共以下 58 个回答
class CQueue { stack_in; stack_out;
Stack
Stack
public CQueue() {
stack_in = new Stack<>();
stack_out = new Stack<>();
}
}
/**
* Your CQueue object will be instantiated and called as such:
* CQueue obj = new CQueue();
* obj.appendTail(value);
* int param_2 = obj.deleteHead();
*/
两个栈实现,但要注意删除时必须保证出栈为空才能出栈
class CQueue {
}
时间复杂度O(1)
空间复杂度O(n)
““java stack1;stack2;
class CQueue{
Stackt
Stack
publc 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;
用两个栈实现队列
直接用了C++ STL stack库
【时间复杂度】最好O(1),最差O(n)
【空间复杂度】最好O(1),最差O(n)
时间复杂度为O(1)
空间复杂度为O(n)
用两个栈实现队列
通过栈1进行入队操作,入队前先判断栈2是否为空,不为空则将栈2元素压入栈1,再进行入队
出队时,将栈1元素全部压入栈2,因为先进后出,所以会从栈1的第一个元素出栈
空间复杂度:O(n)
时间复杂度o(n), 空间复杂度o(n)
两个栈
c++ 实现
python 一个输入栈 一个输出栈
时间复杂度O(n)
C++实现
时间复杂度:O(n)
空间复杂度:O(1)
时间复杂度:O(n)
空间复杂度:O(1)
逆向遍历
C++实现
“`c++
class CQueue {
public:
CQueue() {
};
“`
”’c++ stack1; stack2;
class CQueue {
stack
stack
public:
CQueue() {
};
时间复杂度O(1),空间复杂度O(N)
class CQueue { stack1; stack2;
Stack
Stack
public CQueue() {
stack1 = new Stack<>();
stack2 = new Stack<>();
}
}
时间复杂度:o(1)
空间复杂度:o(1)
C++
TC: O(1) & O(n)
SC: O(n)
根据题意
“`c++stk1; stk2;
class CQueue {
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
“`java
时间复杂度:appendTail O(1),空间复杂度O(1)
deleteHead O(n)
空间复杂度:O(n)
一个栈进 ,一个栈出
但是运行结果后的速率有点低,而且感觉个人的思路存在问题,执行一个步骤进行数据回写的话,会造成数据的重复放置,遂找到了另一个方法:stack1进行插入数据存放,stack2进行数据删除。在不为空的情况下直接删除,在为空的情况下去stack1寻找数据,二者都为空的情况下返回-1。如此进行可以避免数据来回重复放置。
这个的速度就有点提升了。
c++解题思路 s1; s2; st1; st2;
class CQueue {
stack
stack
public:
CQueue() {
stack
s1 = st1;
stack
s2 = st2;
}
};
打卡
时间复杂度:O(n)
额外空间复杂度:O(1)
使用inStack和outStack模拟队列:
1.入队时将元素push入inStack中。
2.出队时从outStack中pop出元素,若outStack没有元素:
1)inStack有元素,直接将inStack里的元素pop到outStack中。
2)inStack没有元素,此时代表整个队列没有元素,返回-1。
时间:O(1),从inStack pop到outStack的时间被均摊。
空间:O(n)
【时间O(n),空间O(n)】
一栈负责在队列尾部插入数,二栈负责在队列头部删除数
当二栈空的时候就把一栈的数全部都倒过来,一栈清空
入队时间复杂度O(1), 出队时间复杂度O(n)
思路
代码
时空复杂度:O(n)
1、理清思路,创建两个栈s1,s2
2、入队列的元素都放进s1
3、出队时,(1)如果s2不为空,则将s2中弹出(2)如果s2为空,s1不为空,则将s1中全部弹出并一次放入s2中(3)如果s1、s2都为空,则返回-1
C++实现
时间复杂度:O(1),空间复杂度:O(n)
思路:设计两个栈,一个负责出队,一个负责入队,入队时直接尽负责入队的栈,出队时,先看负责出队的栈是否为空,不为空直接出栈,为空时,将入队栈内的数据弹出,压入出队栈,然后再出栈,当两栈都为空时,返回空。 stack1; stack2;
C++代码
class CQueue {
public:
stack
stack
CQueue() {}
};
思路:两个栈,一个为入队栈,一个为出队栈。有入队操作时,直接压入入队栈;有出队操作时,先看出队栈是否为空,若有元素,则直接删除出队栈栈顶元素并返回;若为空,再看入队栈,若入堆栈有元素则全部压入出队栈,再删除出队栈栈顶元素并返回,若入堆栈没有元素,则返回-1。
这里是Ming的打卡:
一个栈用于输入,一个栈用于输出
一个栈数据转给另一个栈的时机:输出的栈此时为空,否则直接输出
简单
c实现
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;
}
}
class CQueue { stackW; stackT;
//用两个栈实现队列
//队尾 栈顶压入
ArrayDeque
ArrayDeque
}
/**
* Your CQueue object will be instantiated and called as such:
* CQueue obj = new CQueue();
* obj.appendTail(value);
* int param_2 = obj.deleteHead();
*/
c++双栈实现s1,s2;
private:
stack
public:
CQueue() {
“`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;
}
};
C++实现,设置两个栈,插入放到一个栈,当删除时先判断边界
java实现
理解数据结构即可完成。