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 ...
随机推荐
- BZOJ2752: [HAOI2012]高速公路(road)(线段树 期望)
Time Limit: 20 Sec Memory Limit: 128 MBSubmit: 1820 Solved: 736[Submit][Status][Discuss] Descripti ...
- BZOJ1046: [HAOI2007]上升序列(LIS)
Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 5740 Solved: 2025[Submit][Status][Discuss] Descript ...
- Java分享笔记:Map集合(接口)的基本方法程序演示
package pack02; import java.util.*; public class MapDemo { public static void main(String[] args) { ...
- hdu_3501_Calculation 2
Given a positive integer N, your task is to calculate the sum of the positive integers less than N w ...
- linux下 利用 rz 命令上传文件
1. 如何安装? 1)编译安装 root 账号登陆后,依次执行以下命令: # cd /tmp # wget http://www.ohse.de/uwe/releases/lrzsz-0.12.20 ...
- docker启用镜像常用脚本
语法:docker run [OPTIONS] IMAGE [COMMAND] [ARG...] OPTIONS说明:-a stdin: 指定标准输入输出内容类型,可选 STDIN/STDOUT/ST ...
- linux基础命令3(man)
Type:显示指定的命令是那种类型. Linux下有两种模式的时间 date:用于系统时间管理.(软件操作的系统时 ...
- 005--Django2.0的路由层
URL配置就像Django所支撑的网站目录,它的本质是每条URL调用的视图函数的映射表,每一个请求执行对应的视图函数. 1.简单的路由配置 from django.contrib import ad ...
- PTA 7-10(图) 旅游规划 最短路问题
7-10(图) 旅游规划 (25 分) 有了一张自驾旅游路线图,你会知道城市间的高速公路长度.以及该公路要收取的过路费.现在需要你写一个程序,帮助前来咨询的游客找一条出发地和目的地之间的最短路径.如果 ...
- POJ:3616-Milking Time
Milking Time Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12324 Accepted: 5221 Descrip ...