Java [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 emptyoperations 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).
解题思路:
代码如下:
class MyStack {
Queue<Integer> queue = new LinkedList<Integer>();
// Push element x onto stack.
public void push(int x) {
queue.offer(x);
}
// Removes the element on top of the stack.
public void pop() {
int size = queue.size();
for(int i = 1; i < size; i++)
queue.offer(queue.poll());
queue.poll();
}
// Get the top element.
public int top() {
int size = queue.size();
for(int i = 1; i < size; i++)
queue.offer(queue.poll());
int head = queue.poll();
queue.offer(head);
return head;
}
// Return whether the stack is empty.
public boolean empty() {
return queue.isEmpty();
}
}
Java [Leetcode 225]Implement Stack using Queues的更多相关文章
- 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 ...
- (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 ...
随机推荐
- div+css的前端工程师的价值体现在哪些方面?
个人认为前端工程师正慢慢演变为产品工程师.wap app, 响应性UI等以html5技术为基础的开发将成为前端工程师的主要工作内容,解决产品跨平台跨设备的实现问题.Javascript, HTML, ...
- 20160728noip模拟赛zld
前言:单独对题面描述的评分-> [题解]把相邻长度为2的子串两两连边,跑欧拉路 /*明天再写,先贴一份方老师代码压压惊*/ #include<map> #include<sta ...
- C# 实现:将一个文件夹下的.png图片全部移动到另一个文件夹
如题,代码如下: using System; using System.IO; public class FileMove { public FileMove() { // TODO: } // co ...
- C++ Primer笔记整理
1. 迭代器:迭代器是一种对象,它可以看做是游标,用来遍历标准模板库中的部分或者全部元素. 每个迭代器指向容器中确定的地址,此外,迭代器还提供一些基本操作符:*.++.==.!=.=. 2. 模板:是 ...
- Git教程之版本回退(4)
现在,我们已经学会了修改文件,然后把修改提交到Git版本库,现在再次修改readme.txt文件如下:
- Android Handler使用实例
本文主要介绍Android中Handler的简单使用方法,Handler跟多线程,消息队列联系很紧密,在平常的实际程序开发中比较常见.本文分为4个简单的例子来学校handler Handler使用例1 ...
- 腾讯大讲堂ppt全集
腾讯大讲堂ppt全集 腾讯大讲堂ppt全集资料下载 腾讯大讲堂ppt1-62资料下载 最新最全的腾讯大讲堂ppt全集 腾讯大讲堂ppt全集资料下载 腾讯大讲堂ppt1-62资料下载地址 http:// ...
- Java API —— TreeSet类
1.TreeSet类 1)TreeSet类概述 使用元素的自然顺序对元素进行排序 或者根据创建 set 时提供的 Comparator 进行排序 ...
- The type sun.management.ManagementFactory is not visible
Eclipse默认将这些受访问限制的API设成了Error.解决方法:只要将Windows---Preferences---Java--Complicer---Errors/Warings里面的Dep ...
- laravel url管理与使用
获取当前URL 获取当前URL有两种方式,URL::current()或URL::full(),区别是返不返回GET参数如 Route::get('/current/url',function() { ...