LeetCode_225-Implement Stack using Queues
使用队列实现栈的操作
class MyStack {
public:
/** Initialize your data structure here. */
MyStack() { } /** Push element x onto stack. */
void push(int x) {
if(!queueA.empty()) {
queueA.push(x);
}
else {
queueB.push(x);
}
} /** Removes the element on top of the stack and returns that element. */
int pop() {
int nTmp = ;
if(queueA.empty()) {
while(queueB.size() > ) {
queueA.push(queueB.front());
queueB.pop();
}
nTmp = queueB.front();
queueB.pop();
}
else {
while(queueA.size() > ) {
queueB.push(queueA.front());
queueA.pop();
}
nTmp = queueA.front();
queueA.pop();
}
return nTmp;
} /** Get the top element. */
int top() {
int nTmp = ;
if(queueA.empty()) {
while(!queueB.empty()) {
nTmp = queueB.front();
queueA.push(queueB.front());
queueB.pop();
}
}
else {
while(!queueA.empty()) {
nTmp = queueA.front();
queueB.push(queueA.front());
queueA.pop();
}
} return nTmp;
} /** Returns whether the stack is empty. */
bool empty() {
if(queueA.empty() && queueB.empty()) {
return true;
}
return false;
} protected:
queue<int> queueA;
queue<int> queueB;
};
可关注公众号了解更多的面试技巧
LeetCode_225-Implement Stack using Queues的更多相关文章
- leetcode:Implement Stack using Queues 与 Implement Queue using Stacks
一.Implement Stack using Queues Implement the following operations of a stack using queues. push(x) - ...
- 【LeetCode】232 & 225 - Implement Queue using Stacks & Implement Stack using Queues
232 - Implement Queue using Stacks Implement the following operations of a queue using stacks. push( ...
- 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 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 ...
- Implement Queue by Two Stacks & Implement Stack using Queues
Implement Queue by Two Stacks Implement the following operations of a queue using stacks. push(x) -- ...
- [LeetCode] Implement Stack using Queues 用队列来实现栈
Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...
- Java for LeetCode 225 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)Implement Stack using Queues
Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...
- (easy)LeetCode 225.Implement Stack using Queues
Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...
随机推荐
- Java并发编程(一):进程和线程之由来
转自:http://www.cnblogs.com/dolphin0520/p/3910667.html 在前面,已经介绍了Java的基础知识,现在我们来讨论一点稍微难一点的问题:Java并发编程.当 ...
- centos7 kubernetes单机安装
单机版的kubernetes 适合初学者,对kuber有个很好的入门. 因为centos系统内置了安装源.我们可以直接安装 1.yum install -y etco kubernetes 2.whe ...
- Go语言基础之并发
并发是编程里面一个非常重要的概念,Go语言在语言层面天生支持并发,这也是Go语言流行的一个很重要的原因. Go语言中的并发编程 并发与并行 并发:同一时间段内执行多个任务(你在用微信和两个女朋友聊天) ...
- 019 模块2-time库的使用
目录 一.概述 二.time库基本介绍 2.1 time库概述 三.时间获取 四.时间格式化 4.1 格式化控制符 4.2 时间格式化-代码 五.程序计时应用 5.1 程序计时 六.单元小结 参考:h ...
- Filter过滤器学习
一.Filter简介 Filter也称之为过滤器,它是Servlet技术中最激动人心的技术,WEB开发人员通过Filter技术,对web服务器管理的所有web资源:例如Jsp, Servlet, 静态 ...
- 配置Linux使用LDAP用户认证
配置Linux使用LDAP用户认证 本文首发:https://www.cnblogs.com/somata/p/LinuxLDAPUserAuthentication.html 我这里使用的是Cent ...
- SpringBoot 2 快速整合 | Hibernate Validator 数据校验
概述 在开发RESTFull API 和普通的表单提交都需要对用户提交的数据进行校验,例如:用户姓名不能为空,年龄必须大于0 等等.这里我们主要说的是后台的校验,在 SpringBoot 中我们可以通 ...
- Spring Boot 配置文件和命令行配置
Spring Boot 属于约定大于配置,就是说 Spring Boot 推荐不做配置,很多都是默认配置,但如果想要配置系统,使得软件符合业务定义,Spring Boot 可以通过多种方式进行配置. ...
- 获取contenteditable区域光标所在位置信息
在我们使用contenteditable编辑时,有时需要光标位置的信息. <div contenteditable="true" style="min-height ...
- charles 访问控制设置
本文参考:charles 访问控制设置 charles 访问控制设置 access control settings 访问账户设置: 这里可以配置连接到charles时的一些配置: 这个访问控制确定谁 ...