|
|
|
|
/*/*******************************************************************************
|
|
|
|
|
** **
|
|
|
|
|
** Jiedi(China nanjing)Ltd. **
|
|
|
|
|
** 创建:丁宋涛 夏曹俊,此代码可用作为学习参考 **
|
|
|
|
|
*******************************************************************************/
|
|
|
|
|
|
|
|
|
|
/*****************************FILE INFOMATION***********************************
|
|
|
|
|
**
|
|
|
|
|
** Project : 算法设计与编程实践---基于leetcode的企业真题库
|
|
|
|
|
** Contact : xiacaojun@qq.com
|
|
|
|
|
** 博客 : http://blog.csdn.net/jiedichina
|
|
|
|
|
** 视频课程 : 网易云课堂 http://study.163.com/u/xiacaojun
|
|
|
|
|
腾讯课堂 https://jiedi.ke.qq.com/
|
|
|
|
|
csdn学院 https://edu.csdn.net/course/detail/25037
|
|
|
|
|
** 51cto学院 http://edu.51cto.com/lecturer/index/user_id-100013755.html
|
|
|
|
|
** 老夏课堂 http://www.laoxiaketang.com
|
|
|
|
|
**
|
|
|
|
|
** 算法设计与编程实践---基于leetcode的企业真题库 课程群 :296249312 加入群下载代码和交流
|
|
|
|
|
** 微信公众号 : jiedi2007
|
|
|
|
|
** 头条号 : 夏曹俊
|
|
|
|
|
**
|
|
|
|
|
*****************************************************************************
|
|
|
|
|
//!!!!!!!!! 算法设计与编程实践---基于leetcode的企业真题库 课程 QQ群:296249312 下载代码和交流*/
|
|
|
|
|
/*
|
|
|
|
|
* @lc app=leetcode.cn id=232 lang=cpp
|
|
|
|
|
*
|
|
|
|
|
* [232] 用栈实现队列
|
|
|
|
|
*/
|
|
|
|
|
class MyQueue {
|
|
|
|
|
public:
|
|
|
|
|
/** Initialize your data structure here. */
|
|
|
|
|
MyQueue() {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Push element x to the back of queue. */
|
|
|
|
|
void push(int x) { // 将元素放入栈底
|
|
|
|
|
stack<int> temp;
|
|
|
|
|
while(s.empty() == false){
|
|
|
|
|
temp.push(s.top());
|
|
|
|
|
s.pop();
|
|
|
|
|
}
|
|
|
|
|
s.push(x);
|
|
|
|
|
while(temp.empty() == false){
|
|
|
|
|
s.push(temp.top());
|
|
|
|
|
temp.pop();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Removes the element from in front of queue and returns that element. */
|
|
|
|
|
int pop() {
|
|
|
|
|
int tmp = s.top();
|
|
|
|
|
s.pop();
|
|
|
|
|
return tmp;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Get the front element. */
|
|
|
|
|
int peek() {
|
|
|
|
|
return s.top();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Returns whether the queue is empty. */
|
|
|
|
|
bool empty() {
|
|
|
|
|
return s.empty();
|
|
|
|
|
}
|
|
|
|
|
private:
|
|
|
|
|
stack<int> s,temp;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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();
|
|
|
|
|
* bool param_4 = obj->empty();
|
|
|
|
|
*/
|
|
|
|
|
|