【LeetCode】232 & 225 - Implement Queue using Stacks & Implement Stack using Queues
2015-08-02 13:11
495 查看
232 - Implement Queue using Stacks
Implement the following operations of a queue using stacks.
push(x) -- Push element x to the back of queue.
pop() -- Removes the element from in front of queue.
peek() -- Get the front element.
empty() -- Return whether the queue is empty.
Notes:
You must use only standard operations of a stack -- which means only
Depending on your language, stack may not be supported natively. You may simulate a stack by using a list or deque (double-ended queue), as long as you use only standard operations of a stack.
You may assume that all operations are valid (for example, no pop or peek operations will be called on an empty queue).
225 - Implement Stack using Queues
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
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).
Implement the following operations of a queue using stacks.
push(x) -- Push element x to the back of queue.
pop() -- Removes the element from in front of queue.
peek() -- Get the front element.
empty() -- Return whether the queue is empty.
Notes:
You must use only standard operations of a stack -- which means only
push to top,
peek/pop from top,
size, and
is emptyoperations are valid.
Depending on your language, stack may not be supported natively. You may simulate a stack by using a list or deque (double-ended queue), as long as you use only standard operations of a stack.
You may assume that all operations are valid (for example, no pop or peek operations will be called on an empty queue).
class Queue { public: // Push element x to the back of queue. void push(int x) { stk.push(x); } // Removes the element from in front of queue. void pop(void) { stack<int> temp; while(stk.size()!=1){ int x=stk.top(); stk.pop(); temp.push(x); } stk.pop(); while(!temp.empty()){ int x=temp.top(); temp.pop(); stk.push(x); } } // Get the front element. int peek(void) { stack<int> temp; while(stk.size()!=1){ int x=stk.top(); stk.pop(); temp.push(x); } int x=stk.top(); while(!temp.empty()){ int x=temp.top(); temp.pop(); stk.push(x); } return x; } // Return whether the queue is empty. bool empty(void) { return stk.empty(); } private: stack<int> stk; }
225 - Implement Stack using Queues
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
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).
class Stack { public: // Push element x onto stack. void push(int x) { q.push(x); } // Removes the element on top of the stack. void pop() { queue<int> temp; while(q.size()!=1){ int x=q.front(); q.pop(); temp.push(x); } q=temp; } // Get the top element. int top() { queue<int> temp; while(q.size()!=1){ int x=q.front(); q.pop(); temp.push(x); } int x=q.front(); q=temp; q.push(x); return x; } // Return whether the stack is empty. bool empty() { return q.empty(); } private: queue<int> q; };
相关文章推荐
- 在iOS 8中使用UIAlertController
- 【转】UITableView使用详解
- 洛谷1373 小a和uim之大逃离
- easyui dataGrid接收嵌套JSON
- easyui dialog子窗口关闭父窗口
- android:layout_centerHorizontal="true"和 android:gravity="center_horizontal"什么区别?
- Selenium2学习-026-WebUI自动化实战实例-024-获取页面元素
- uuid (guid)全局唯一标识符
- Android SDK Tools Platform-tools Build-tools
- hdu 5329 Question for the Leader 枚举,划分图为k个联通的子图
- Gym 100570E : Palindrome Query
- IOS开发之UIScrollViewDelegate详解
- iOS 人机交互设计(开发)指南及一些综合知识整合
- Codeforces 558 D. Guess Your Way Out! II
- Selenium2学习-025-WebUI自动化实战实例-023-页面快照截图应用之一 -- 常规截图(全页面)
- 1099. Build A Binary Search Tree (30)
- Web富文本编辑器---UEditor
- easyUI 验证控件应用、自定义、扩展验证 手机号码或电话话码格式
- Android Api Demos登顶之路(十四)Quick Contacts
- 猫猫学iOS 之微博项目实战(7)程序启动新特性用UICollectionViewController实现