225 Implement Stack using Queues(用队列实现栈Medium)
题目意思:用队列实现栈,push(),pop(),top(),empty()
思路:用两个queue,pop时将一个queue的元素pop再push到另一个队列,queue只留最后一个元素,并pop,再将目标队列变为另一个
ps:用栈实现队列,参考剑指offer
class Stack {
private:
queue<int> q[];
int flag=;
public:
// Push element x onto stack.
void push(int x) {
q[flag].push(x);
}
// Removes the element on top of the stack.
void pop() {
while(q[flag].size()>){
q[-flag].push(q[flag].front());
q[flag].pop();
}
q[flag].pop();
flag=-flag;
}
// Get the top element.
int top() {
return q[flag].back();
}
// Return whether the stack is empty.
bool empty() {
if(q[flag].empty()){
return true;
}
return false;
}
};
225 Implement Stack using Queues(用队列实现栈Medium)的更多相关文章
- LeetCode 225 Implement Stack using Queues 用队列实现栈
1.两个队列实现,始终保持一个队列为空即可 class MyStack { public: /** Initialize your data structure here. */ MyStack() ...
- [LeetCode] 225. Implement Stack using Queues 用队列来实现栈
Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...
- LeetCode OJ:Implement Stack using Queues(队列实现栈)
Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...
- Implement Stack using Queues 用队列实现栈
Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...
- leetcode 155. Min Stack 、232. Implement Queue using Stacks 、225. Implement Stack using Queues
155. Min Stack class MinStack { public: /** initialize your data structure here. */ MinStack() { } v ...
- 232. Implement Queue using Stacks,225. Implement Stack using Queues
232. Implement Queue using Stacks Total Accepted: 27024 Total Submissions: 79793 Difficulty: Easy Im ...
- LeetCode 225 Implement Stack using Queues(用队列来实现栈)(*)
翻译 用队列来实现栈的例如以下操作. push(x) -- 将元素x加入进栈 pop() -- 从栈顶移除元素 top() -- 返回栈顶元素 empty() -- 返回栈是否为空 注意: 你必须使用 ...
- 225 Implement Stack using Queues 队列实现栈
Implement the following operations of a stack using queues. push(x) -- Push element x onto stack.pop ...
- [LeetCode] Implement Stack using Queues 用队列来实现栈
Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...
随机推荐
- Linux企业级开发技术(1)——epoll企业级开发之简介
Epoll是当前在 Linux 下开发大规模并发网络程序的热门人选, Epoll 在 Linux2.6 内核中正式引入.和 select 相似,是高效 I/O 多路复用技术. 其实在 Linux 下设 ...
- 【中途相遇法】【STL】BAPC2014 K Key to Knowledge (Codeforces GYM 100526)
题目链接: http://codeforces.com/gym/100526 http://acm.hunnu.edu.cn/online/?action=problem&type=show& ...
- HDOJ 2071 Max Num
Problem Description There are some students in a class, Can you help teacher find the highest studen ...
- Java---对象与类的封装
一.类和对象: package cn.hncu.Myclasslearn; /** * * @author hncu_chx * * Mylove amin */ /**类是一种数据类型,声明一个类就 ...
- HDU 4751 Divide Groups 2013 ACM/ICPC Asia Regional Nanjing Online
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4751 题目大意:判断一堆人能否分成两组,组内人都互相认识. 解题思路:如果两个人不是相互认识,该两人之 ...
- 什么是ARC
arc就是自动引用计算.英文名Automatic Reference Counting.在一开始的IOS开发中,内存管理是需要手动的,对某个资源的引用,引用后就对其计算+1,当不再使用就-1,当计算为 ...
- Away3D 4.1.4 中实现骨骼绑定
骨骼的绑定归根结底就是将目标骨骼的位置以及旋转数据,同步给要绑定的显示对象. 先来看BindingTag.as package away3d.entities { import away3d. ...
- winform combobox控件绑定 分类: WinForm 2014-04-17 14:34 118人阅读 评论(0) 收藏
想要达到的效果:把数据库中的一列数据绑定到combobox控件中. 数据库表:T_Task//任务表 列名:Task_Name//名称 主键:Task_ID combobox控件名称:cbName 解 ...
- iphone匹配邮箱的正则表达式
NSString *str = [NSString stringWithString:@"\\b([a-zA-Z0-9%_.+\\-]+)@([a-zA-Z0-9.\\-]+?\\.[a-z ...
- Linux下一个Redis启动/关闭/重新启动服务脚本
脚本功能: 实现redis单机多实例情况下的正常启动.关闭.重新启动单个redis实例.完毕系统标准服务的下面经常使用功能: start|stop|status|restart 注:redis程序代 ...