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 emptyoperations 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 ...
随机推荐
- rest_framwork组件
rest framework介绍 (CBV(class base views)) 在开发REST API的视图中,虽然每个视图具体操作的数据不同,但增.删.改.查的实现流程基本套路化,所以这部分代码也 ...
- MySQL的where条件优化
where 条件优化 适合select delete update 1.避免无用的括号 ((a AND b) AND c OR (((a AND b) AND (c AND d)))) -> ...
- CentOS 6.5通过yum安装 MySQL-5.5
1.安装mysql-5.5的yum源 rpm -ivh http://repo.mysql.com/yum/mysql-5.5-community/el/6/x86_64/mysql-communit ...
- php-5.6.26源代码 - 扩展模块的加载、注册
// main实现在文件 php-5.6.26\sapi\cgi\cgi_main.c int main(int argc, char *argv[]) { .... cgi_sapi_module- ...
- ADSL_自动拨号源码(Delphi),已经测试通过
下载地址: http://files.cnblogs.com/lwm8246/ADSL_%E8%87%AA%E5%8A%A8%E6%8B%A8%E5%8F%B7.rar
- 多通道CNN
在读Convolutional Neural Networks for Sentence Classification 这个文章的时候,它在论文中提出一种模型变种就是 CNN-multichannel ...
- Python 编码格式的使用
编码史 ASCII > Unicode > UTF-8 Unicode支持多语言,UTF-8自动转换长短细节节省空间 在计算机内存中,统一使用Unicode编码,当需要保存到硬盘或者需要传 ...
- 笔记-scrapy-signal
笔记-scrapy-signal 1. scrapy singal 1.1. 信号机制 scrapy的信号机制主要由三个模块完成 signals.py 定义信号量 signalmana ...
- 笔记-git-git服务器安装及配置
笔记-git-git服务器安装及配置 1. GIT服务器简介 Git 可以使用四种主要的协议来传输数据:本地传输,SSH 协议,Git 协议和 HTTP 协议.下面分别介绍一下哪些情形应该使 ...
- wireshark 获取RTP payload
wireshark 抓包获取RTP TS流数据,保存为TS文件 首先解析RTP流 2.点击菜单栏[Statistics]-[RTP]-[Show All Streams] 3.在Wireshark:R ...