LeetCode -- Implement Stacks using Queue
Question:
Implement the following operations of a queue using stacks.
- push(x) -- Push element x to the back of queue.
- pop() -- Removes the element from in front of queue.
- peek() -- Get the front element.
- empty() -- Return whether the queue is empty.
Notes:
- You must use only standard operations of a stack -- which means only
push to top
,peek/pop from top
,size
, andis empty
operations are valid. - Depending on your language, stack may not be supported natively. You may simulate a stack by using a list or deque (double-ended queue), as long as you use only standard operations of a stack.
- You may assume that all operations are valid (for example, no pop or peek operations will be called on an empty queue).
Analysis:
问题描述:用队列模仿一个栈。
思路:用两个队列模仿一个栈。每次要pop或者peek时,使用队列倒换一下,剩下最后一个元素单独处理。当且仅当两个队列都为空时,栈才为空。
Answer:
class MyStack {
Queue<Integer> q1 = new LinkedList<Integer>();
Queue<Integer> q2 = new LinkedList<Integer>(); // Push element x onto stack.
public void push(int x) {
q1.offer(x);
} // Removes the element on top of the stack.
public void pop() {
if(!q1.isEmpty()) {
while(q1.size() > 1) {
int i = q1.poll();
q2.offer(i);
}
q1.poll();
} else {
while(q2.size() > 1) {
int i = q2.poll();
q1.offer(i);
}
q2.poll();
}
} // Get the top element.
public int top() {
if(!q1.isEmpty()) {
while(q1.size() > 1) {
int i = q1.poll();
q2.offer(i);
}
int i = q1.poll();
q2.offer(i);
return i;
} else {
while(q2.size() > 1) {
int i = q2.poll();
q1.offer(i);
}
int i = q2.poll();
q1.offer(i);
return i;
}
} // Return whether the stack is empty.
public boolean empty() {
if(q1.size() == 0 && q2.size() == 0)
return true;
return false;
}
}
LeetCode -- Implement Stacks using Queue的更多相关文章
- [LeetCode] Implement Queue using Stacks 用栈来实现队列
Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of ...
- Leetcode Implement Queue using Stacks
Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of ...
- LeetCode——Implement Queue using Stacks
Description: Implement the following operations of a queue using stacks. push(x) -- Push element x t ...
- LeetCode Implement Queue using Stacks (数据结构)
题意: 用栈来实现队列. 思路: 一个栈是不够的,至少要两个. (1)插入.永远只插入到stack1中(插到栈顶). (2)弹出.如果stack2不为空,直接弹出stack2的栈顶,否则,将stack ...
- [LeetCode] Implement Stack using Queues 用队列来实现栈
Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...
- [LeetCode] Implement Trie (Prefix Tree) 实现字典树(前缀树)
Implement a trie with insert, search, and startsWith methods. Note:You may assume that all inputs ar ...
- [LeetCode] Implement strStr() 实现strStr()函数
Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...
- (leetcode)Implement Stack using Queues
Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...
- [Leetcode] implement strStr() (C++)
Github leetcode 我的解题仓库 https://github.com/interviewcoder/leetcode 题目: Implement strStr(). Returns ...
随机推荐
- Informatica 简单使用
1. Informatica简介 ① Repository manager 主要用来维护资料库的目录,对象,建完对象可以创建demo的folder. ② Administration Console是 ...
- linux下后台运行jar包
#当前窗口退出 项目关闭$ java -jar test.jar #当前窗口关闭 项目关闭$ nohup java -jar test.jar &#当前窗口关闭 项目不关闭$ nohup ja ...
- 微信小程序开发入门学习(1):石头剪刀布小游戏
从今天起开始捣鼓小程序了2018-12-17 10:02:15 跟着教程做了第一个入门实例有兴趣的朋友可以看看: 猜拳游戏布局 程序达到的效果 猜拳游戏的布局是纵向显示了三个组件:文本组件(tex ...
- pycharm快捷键一览
编辑(Editing) Ctrl + Space 基本的代码完成(类.方法.属性)Ctrl + Alt + Space 快速导入任意类Ctrl + Shift + Enter 语句完成Ctrl + P ...
- scrapy--boss直聘
Hi,大家好.有段时间没来更新scrapy爬取实例信息了,前2天同事说爬取拉勾,boss直聘等网站信息比较困难.昨天下午开始着手爬取boss直聘内Python爬虫的信息,比想象中的简单很多. 需要解决 ...
- Java 算法随笔(一)
1. 最大子序列和问题 给定(可能有负数)整数a(1).a(2).……a(n),求 a(1)+a(2)+……+a(j)的最大值. 也就是:在一系列整数中,找出连续的若干个整数,这若干个整数之和最大.有 ...
- JDK学习---深入理解Comparator、TreeSet、TreeMap为什么可以排序
我本来打算仔细的去分析分析TreeSet和TreeMap排序规则,并且从底层实现和数据结构入手.当我去读完底层源码以后,我感觉我就的目标定的太大了,单单就是数据结构就够我自己写很久了,因此我决定先易后 ...
- python搭建友盟以及个推推送web服务器
一.友盟客户端demo: 由于SDK原因,新版Android Studio的Android API 28 Platform无法同步新建项目, 所以我最终选择下载android-studio-bundl ...
- Android stadio 工具使用
android staido 有logcat窗口,她可以显示log信息.还有run窗口. 我以前一直忽略了run窗口,其实蛮重要,蛮好用的.他只会显示你当前运行的进程的log,不用你再去设置filld ...
- runtime如何通过selector找到对应的IMP地址?(分别考虑类方法和实例方法)
每一个类对象中都一个对象方法列表(对象方法缓存) 类方法列表是存放在类对象中isa指针指向的元类对象中(类方法缓存) 方法列表中每个方法结构体中记录着方法的名称,方法实现,以及参数类型,其实selec ...