LeetCode225——Implement Stack using Queues
2015-07-31 17:22
453 查看
Implement the following operations of a stack using queues.
push(x) -- Push element x onto stack.
pop() -- Removes the element on top of the stack.
top() -- Get the top element.
empty() -- Return whether the stack is empty.
Notes:
You must use only standard operations of a queue -- which means only
and
Depending on your language, queue may not be supported natively. You may simulate a queue by using a list or deque (double-ended queue), as long as you use only standard operations of a queue.
You may assume that all operations are valid (for example, no pop or top operations will be called on an empty stack).
Update (2015-06-11):
The class name of the Java function had been updated to MyStack instead of Stack.
实现:
class Stack {
public:
// Push element x onto stack.
void push(int x) {
dq.push_back(x);
last = x;
}
// Removes the element on top of the stack.
void pop() {
if (empty()) return;
vector<int> vec;
while (!empty()) {
vec.push_back(dq.front());
dq.pop_front();
}
for (int i = 0; i < vec.size()-1; i++) {
push(vec[i]);
}
}
// Get the top element.
int top() {
return last;
}
// Return whether the stack is empty.
bool empty() {
return dq.size() == 0;
}
private:
deque<int> dq;
int last;
};
push(x) -- Push element x onto stack.
pop() -- Removes the element on top of the stack.
top() -- Get the top element.
empty() -- Return whether the stack is empty.
Notes:
You must use only standard operations of a queue -- which means only
push to back,
peek/pop from front,
size,
and
is emptyoperations are valid.
Depending on your language, queue may not be supported natively. You may simulate a queue by using a list or deque (double-ended queue), as long as you use only standard operations of a queue.
You may assume that all operations are valid (for example, no pop or top operations will be called on an empty stack).
Update (2015-06-11):
The class name of the Java function had been updated to MyStack instead of Stack.
实现:
class Stack {
public:
// Push element x onto stack.
void push(int x) {
dq.push_back(x);
last = x;
}
// Removes the element on top of the stack.
void pop() {
if (empty()) return;
vector<int> vec;
while (!empty()) {
vec.push_back(dq.front());
dq.pop_front();
}
for (int i = 0; i < vec.size()-1; i++) {
push(vec[i]);
}
}
// Get the top element.
int top() {
return last;
}
// Return whether the stack is empty.
bool empty() {
return dq.size() == 0;
}
private:
deque<int> dq;
int last;
};
相关文章推荐
- 【EasyUi DataGrid】动态加载列
- Selenium2学习-021-WebUI自动化实战实例-019-设置浏览器窗口位置大小
- git guide
- 最新 Arduino 驱动 12接口/户外 LED显示屏/LED点阵屏/LED单元板
- UIViewController视图控制器
- Ueditor使用方法
- LeetCode232——Implement Queue using Stacks
- SPOJ 2713. Can you answer these queries IV(GSS4 线段树)
- Android UI 之 ExpandableListActivity、ExpandableListView
- Selenium2学习-020-WebUI自动化实战实例-018-获取浏览器窗口位置大小
- hdu 4746 Mophues 莫比乌斯反演
- UITableView - 2
- ZOJ 2105 Number Sequence(矩阵快速幂)
- 提高android应用ui运行效率
- Error:Cannot build Artifact :war exploded because it is included into a circular depency
- iOS阶段学习第30天笔记( UIViewController—Delegate(代理) )
- (转)UIViewController中各方法调用顺序及功能详解
- hdu 4396 More lumber is required(最短路)
- [leetcode] Implement Stack using Queues
- UIView常用的一些方法小记之setNeedsDisplay和setNeedsLayout