225. Implement Stack using Queues
代码如下:
class MyStack {
// Push element x onto stack.
Queue<Integer> queue=new ArrayDeque<>(); public void push(int x) {
Queue<Integer> p=new ArrayDeque<>();
int c=queue.size();
while(c!=)
{
p.offer(queue.peek());
queue.remove();
c--;
}
queue.offer(x);
int d=p.size();
while(d!=)
{
queue.offer(p.peek());
p.remove();
d--;
} } // Removes the element on top of the stack.
public void pop() {
queue.remove();
} // Get the top element.
public int top() {
return queue.peek();
} // Return whether the stack is empty.
public boolean empty() {
if(queue.size()==)
return true;
return false;
}
}
225. Implement Stack using Queues的更多相关文章
- 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 ...
- 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 ...
- (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 ...
- 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 ...
- 【一天一道LeetCode】#225. Implement Stack using Queues
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Impleme ...
- 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 ...
随机推荐
- POJ 1083 Moving Tables 思路 难度:0
http://poj.org/problem?id=1083 这道题题意是有若干段线段,每次要求线段不重叠地取,问最少取多少次. 因为这些线段都是必须取的,所以需要让空隙最小 思路: 循环直到线段全部 ...
- PowerMock使用遇到的问题——2
如果在测一个类的某一个方法时,这个方法还调用了此类的其他方法,那么如何指定其他方法的返回值呢? Partial mock local private method or public method i ...
- C#Winform从页面获取数据,传入数据库
环境: 1.SQLite数据库新建数据表,设置相应的字段.(其他数据库形式都相似,using相应数据库的包即可) 2.页面有两个textBox:textBox1.textBox2, 3.一个保存按钮: ...
- 注册表 ReadBool类型和 ReadInteger 的关系
function TRegistry.ReadBool(const Name: string): Boolean; begin Result := ReadInteger(Name) <> ...
- IT公司100题-12-求1+2+…+n
问题描述: 求1+2+…+n,要求不能使用乘除法.for.while.if.else.switch.case等关键字以及条件判断语句(A?B:C). 分析: 利用类的静态变量实现: new一含有n ...
- Xcode6.2创建Empty Application
运行Xcode 6,创建一个Single View Application工程. 创建好后,把工程目录下的Main.storyboard和LaunchScreen.xib删除,扔进废纸篓. 打 ...
- [网络技术][转]PPTP协议解析
PPTP协议大体上可以分为两部分:控制层连接和隧道,下面简要介绍两部分的功能.如果要详细了解PPTP协议请阅读RFC文档. 一. Control Connection Protol 控制层连接是基于T ...
- [流媒体]VLC主要模块
libvlccore vlcthread: vlc线程是libvlccore的重要组成部分,我们在src文件夹下面android.os2.posix.win32等文件夹下包含thread.c文件,说明 ...
- Android-LogCat日志工具(一)
LogCat : Android中一个命令行工具,可以用于得到程序的log信息. 就像你知道一个人的日志.航程,你可以无时无刻知道一个人在干什么. 而LogCat , 就是程序的日志.通过日志,你可以 ...
- LINQ基础(一)
一.学习LINQ需要先了解以下知识点: 1.1 委托 1.2 匿名方法 1.3 Lambda表达式 1.4 扩展方法 二.LINQ原理: from s in names where s.le ...