LeetCode 225 Implement Stack using Queues(用队列来实现栈)(*)
翻译
用队列来实现栈的例如以下操作。
push(x) —— 将元素x加入进栈
pop() —— 从栈顶移除元素
top() —— 返回栈顶元素
empty() —— 返回栈是否为空
注意:
你必须使用一个仅仅有标准操作的队列。
也就是说,仅仅有push/pop/size/empty等操作是有效的。
队列可能不被原生支持。这取决于你所用的语言。
仅仅要你仅仅是用queue的标准操作,你能够用list或者deque(double-ended queue)来模拟队列。
你能够如果全部的操作都是有效的(比如,pop或peek操作不会被用在空栈上)。
原文
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 empty operations 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).
分析
对栈和队列不清楚的话。能够先看这篇图文介绍:【算法】7 分不清栈和队列?一张图给你完总体会
至于这道题目。有一道很很相似的题目。
本题是用队列来实现栈,以下这题是用栈来实现队列。由于在上一篇中解说过,原理是一样的,大家能够自己看看:LeetCode 232 Implement Queue using Stacks(用栈来实现队列)(*)
代码
class Stack {
public:
queue<int> q, q2;
// Push element x onto stack.
void push(int x) {
q.push(x);
}
// Removes the element on top of the stack.
void pop() {
if (q.size() == 1) q.pop();
else {
while (q.size() > 1) {
q2.push(q.front());
q.pop();
}
q.pop();
while (q2.size() > 0) {
q.push(q2.front());
q2.pop();
}
}
}
// Get the top element.
int top() {
if (q.size() == 1) return q.front();
while (q.size() > 1) {
q2.push(q.front());
q.pop();
}
int temp = q.front();
q2.push(q.front());
q.pop();
while (q2.size() > 0) {
q.push(q2.front());
q2.pop();
}
return temp;
}
// Return whether the stack is empty.
bool empty() {
return q.empty();
}
};
LeetCode 225 Implement Stack using Queues(用队列来实现栈)(*)的更多相关文章
- [LeetCode] 225. Implement Stack using Queues 用队列来实现栈
Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...
- LeetCode 225 Implement Stack using Queues 用队列实现栈
1.两个队列实现,始终保持一个队列为空即可 class MyStack { public: /** Initialize your data structure here. */ MyStack() ...
- [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 for LeetCode 225 Implement Stack using Queues
Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...
- Leetcode 225 Implement Stack using Queues
Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...
- Java [Leetcode 225]Implement Stack using Queues
题目描述: Implement the following operations of a stack using queues. push(x) -- Push element x onto sta ...
- Leetcode 225 Implement Stack using Queues STL
用两个队列去实现栈,这里我使用了队列数组q[2],在所有的过程中保证一个队列是空的 push时插入到空的队列中,然后将队列中的元素移到另一个队列中 pop时从不空的队列中pop() peek时从不空的 ...
- 225 Implement Stack using Queues(用队列实现栈Medium)
题目意思:用队列实现栈,push(),pop(),top(),empty() 思路:用两个queue,pop时将一个queue的元素pop再push到另一个队列,queue只留最后一个元素,并pop, ...
随机推荐
- Java Callable接口与Future接口的两种使用方式
Java Callable.Future的两种使用方式Callable+Futurepublic class Test { public static void main(String[] args) ...
- java常见反编译工具
1.Java反编译插件 —— Jadclipse JadClipse是Jad的Eclipse插件,是一款非常实用而且方便地Java反编译插件,我们只需将下载的插件包复制到eclipse的plugins ...
- Servlet与JSP的区别(转)
原文链接:Servlet与JSP的区别 两者之间的联系和区别 [1]JSP第一次运行的时候会编译成Servlet,驻留在内存中以供调用. [2]JSP是web开发技术,Servlet是服务器端运用的小 ...
- C# 遍历文件夹非递归实现(采用队列的广度优先算法)(转)
一.实现思路: 1. 创建一个队列(使用C# 队列类 Queue,需要使用命名空间 System.Collections.Generic): 2. 把起始文件夹名称排入队中: 3. 检查队列中是否有文 ...
- 修改IP地址的PowerShell
$wmi = Get-WmiObject win32_networkadapterconfiguration -filter "ipenabled = 'true'" $wmi.E ...
- AI 经典书单 | 人工智能学习该读哪些书
转载 2018年01月16日 00:00:00 人工智能相关岗位中,涉及到的内容包含: 算法.深度学习.机器学习.自然语言处理.数据结构.Tensorflow.Python .数据挖掘.搜索开发. ...
- 亚马逊AWS CentOS7(linux)改为用户名密码登录
1.进入AWS系统 略 系统为:centos 7 fox.风 2.设置ROOT密码 sudo passwd root 1 3.修改配置文件 sudo vim /etc/ssh/sshd_config ...
- centos7 tomcat9
1.下载 下载 apache-tomcat-9.0.0.M4.tar.gz 文件: wget http://mirror.bit.edu.cn/apache/tomcat/tomcat-9/v9.0 ...
- Tensorflow中张量数据类型的转换
https://blog.csdn.net/Tramac/article/details/74942587 字符串转为数字: tf.string_to_number (string_tensor, o ...
- yield python
原文:http://pyzh.readthedocs.io/en/latest/the-python-yield-keyword-explained.html 3. (译)Python关键字yield ...