Java for LeetCode 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
, andis 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).
Update (2015-06-11):
The class name of the Java function had been updated to MyStack instead of Stack.
解题思路:
用Queue的实例LinkedList实现,JAVA实现如下:
class MyStack {
LinkedList<Integer> queue; MyStack() {
this.queue = new LinkedList<Integer>();
} // Push element x onto stack.
public void push(int x) {
queue.add(x);
} // Removes the element on top of the stack.
public void pop() {
queue.remove(queue.size()-1);
} // Get the top element.
public int top() {
return queue.getLast();
} // Return whether the stack is empty.
public boolean empty() {
return queue.isEmpty();
}
}
Java for LeetCode 225 Implement Stack using Queues的更多相关文章
- 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 用队列来实现栈
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 ...
- 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(用队列来实现栈)(*)
翻译 用队列来实现栈的例如以下操作. push(x) -- 将元素x加入进栈 pop() -- 从栈顶移除元素 top() -- 返回栈顶元素 empty() -- 返回栈是否为空 注意: 你必须使用 ...
- Leetcode 225 Implement Stack using Queues STL
用两个队列去实现栈,这里我使用了队列数组q[2],在所有的过程中保证一个队列是空的 push时插入到空的队列中,然后将队列中的元素移到另一个队列中 pop时从不空的队列中pop() peek时从不空的 ...
- LeetCode 225 Implement Stack using Queues 用队列实现栈
1.两个队列实现,始终保持一个队列为空即可 class MyStack { public: /** Initialize your data structure here. */ MyStack() ...
- 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 ...
随机推荐
- 昨天的这个先补上--这个是关于 JQ 的移动 和 渐变特效的点击事件
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- Java 线程Thread.Sleep详解
我们可能经常会用到 Thread.Sleep 函数来使线程挂起一段时间.那么你有没有正确的理解这个函数的用法呢? 思考下面这两个问题: 1.假设现在是 2008-4-7 12:00:00.000,如果 ...
- c++模板
1.从 python 说起 def add(a, b): return a + b; print add(3.1, 5.1); #8.2 print add("abc", &quo ...
- 正确使用Python logging
这篇文章主要参考: http://victorlin.me/posts/2012/08/26/good-logging-practice-in-python ===================== ...
- 包介绍 - UriTemplates (用于处理格式化Uri模板)
UriTemplates 用于处理格式化Uri模板 PM> Install-Package Tavis.UriTemplates 设置Uri Path Segment [Fact] public ...
- MySQL性能优化的最佳经验,随时补充
1.为查询优化你的查询 大多数的MySQL服务器都开启了查询缓存.这是提高性最有效的方法之一,而且这是被MySQL的数据库引擎处理的.当有很多相同的查询被执行了多次的时候,这些查询结果会被放到一个缓存 ...
- lwfs指定特定目录输出
在特定节点启lwfs服务,输出特定的目录 在[root@devcpucs ~]# 节点启lwfs服务,输出指定目录/home/export/online1/systest/swcpucs 1.将gio ...
- LYDSY模拟赛day9 2048
/* 大模拟题,做的时候思路还是比较清晰的 */ #include<iostream> #include<cstdio> #include<string> #inc ...
- poj3070 Fibonacci
Description In the Fibonacci integer sequence, F0 = 0, F1 = 1, and Fn = Fn − 1 + Fn − 2 for n ≥ 2. F ...
- 【转】关于URL编码/javascript/js url 编码/url的三个js编码函数
来源:http://www.cnblogs.com/huzi007/p/4174519.html 关于URL编码/javascript/js url 编码/url的三个js编码函数escape(),e ...