本文最后更新于627 天前,其中的信息可能已经过时,如有错误请发送邮件到tomding1065@gmail.com
题目链接/文章讲解/视频讲解:https://programmercarl.com/0225.%E7%94%A8%E9%98%9F%E5%88%97%E5%AE%9E%E7%8E%B0%E6%A0%88.html
1.感觉自己的基础确实是有点薄弱,就是这些个库函数,基础的数据结构里面都有什么函数,我都不是很清楚,感觉stl的内容也是一知半解,写两个队列模拟栈,我说实话我自己是没啥思路的,但是看了代码随想录的内容和代码,还是能让我豁然开朗。跟着思路自己也实现了一遍,发现自己确实是有进步。有时间还是要补充一下stl和基本的数据结构的知识点。
CPP
class MyStack {
public:
MyStack() {
}
queue<int>qIn;
queue<int>qCp;
void push(int x) {
qIn.push(x);
}
int pop() {
int size = qIn.size();
size--;
while(size--){
qCp.push(qIn.front());
qIn.pop();
}
int res = qIn.front();
qIn.pop();
qIn = qCp;
while(!qCp.empty()){
qCp.pop();
}
return res;
}
int top() {
int size = qIn.size();
size--;
while(size--){
qCp.push(qIn.front());
qIn.pop();
}
int res = qIn.front();
qCp.push(res);
qIn.pop();
qIn = qCp;
while(!qCp.empty()){
qCp.pop();
}
return res;
}
bool empty() {
return qIn.empty();
}
};
/**
* Your MyStack object will be instantiated and called as such:
* MyStack* obj = new MyStack();
* obj->push(x);
* int param_2 = obj->pop();
* int param_3 = obj->top();
* bool param_4 = obj->empty();
*/