博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[LeetCode]Implement Queue using Stacks
阅读量:2236 次
发布时间:2019-05-09

本文共 1413 字,大约阅读时间需要 4 分钟。

解题思路:
1,使用2个stack,一个叫PushStack,一个叫PopStack。
2,每当Queue新进来一个element时,就把它加入到PushStack,
3,当Queue执行Pop操作时,从PopStack取出element。
4,当PopStack为空时,把PushStack的元素一个一个的加入到PopStack
数据如何从pushStack到popStack
1,popStack必须为空,pushStack非空;
2,element = pushStack.top()
     popStack.push(element)
     pushStack.pop()
3,反复执行步骤2,直到pushStack为空
// 编译错误

1,Stack不是一个已经定义的类型。应该小写s的  stack 

class Queue {public:    // Push element x to the back of queue.    void push(int x) {        pushStack.push(x);    }    // Removes the element from in front of queue.    void pop(void) {        if (empty()){            // EXCLUDE IN THIS PROBLEM        }        if (popStack.empty() && !pushStack.empty()){            tansportation();        }        return popStack.pop();    }    // Get the front element.    int peek(void) {        if (empty()){            // EXCLUDE IN THIS PROBLEM        }        if (popStack.empty() && !pushStack.empty()){            tansportation();        }        return popStack.top();    }    // Return whether the queue is empty.    bool empty(void) {        return pushStack.empty() && popStack.empty();    }    void tansportation(void){        if (popStack.empty() && !pushStack.empty()){            while(!pushStack.empty()){                int elem = pushStack.top();                popStack.push(elem);                pushStack.pop();            }        }    }private:    stack
pushStack; stack
popStack;};

转载地址:http://rcpbb.baihongyu.com/

你可能感兴趣的文章
用 LSTM 做时间序列预测的一个小例子
查看>>
用 LSTM 来做一个分类小问题
查看>>
详解 LSTM
查看>>
按时间轴简述九大卷积神经网络
查看>>
详解循环神经网络(Recurrent Neural Network)
查看>>
为什么要用交叉验证
查看>>
用学习曲线 learning curve 来判别过拟合问题
查看>>
用验证曲线 validation curve 选择超参数
查看>>
用 Grid Search 对 SVM 进行调参
查看>>
用 Pipeline 将训练集参数重复应用到测试集
查看>>
PCA 的数学原理和可视化效果
查看>>
机器学习中常用评估指标汇总
查看>>
什么是 ROC AUC
查看>>
Bagging 简述
查看>>
详解 Stacking 的 python 实现
查看>>
简述极大似然估计
查看>>
用线性判别分析 LDA 降维
查看>>
用 Doc2Vec 得到文档/段落/句子的向量表达
查看>>
使聊天机器人具有个性
查看>>
使聊天机器人的对话更有营养
查看>>