当前位置:网站首页>Leetcode - 232 realize queue with stack (design double stack to realize queue)
Leetcode - 232 realize queue with stack (design double stack to realize queue)
2022-07-25 15:40:00 【Cute at the age of three @d】


Queues are implemented with two stacks

class MyQueue {
// Use two stacks to implement queues
// Main stack
private Deque<Integer> deque1;
// Auxiliary stack
private Deque<Integer> deque2;
public MyQueue() {
deque1 = new LinkedList<>();
deque2 = new LinkedList<>();
}
public void push(int x) {
while(deque1.size() != 0){
deque2.offerLast(deque1.pollLast());
}
deque1.offerLast(x);
while(deque2.size() != 0){
deque1.offerLast(deque2.pollLast());
}
}
public int pop() {
return deque1.pollLast();
}
public int peek() {
return deque1.peekLast();
}
public boolean empty() {
return deque1.size() == 0;
}
}
/** * Your MyQueue object will be instantiated and called as such: * MyQueue obj = new MyQueue(); * obj.push(x); * int param_2 = obj.pop(); * int param_3 = obj.peek(); * boolean param_4 = obj.empty(); */
边栏推荐
猜你喜欢

Take you to create your first C program (recommended Collection)

Window system black window redis error 20creating server TCP listening socket *: 6379: listen: unknown error19-07-28

ML - natural language processing - Basics

matlab 优化工具 manopt 安装

JVM知识脑图分享

LeetCode - 303 区域和检索 - 数组不可变 (设计 前缀和数组)

解决vender-base.66c6fc1c0b393478adf7.js:6 TypeError: Cannot read property ‘validate‘ of undefined问题

Get the ask code corresponding to the key pressed by the keyboard

Pytorch学习笔记-刘二老师RNN高级篇-代码注释及结果

LeetCode - 359 日志速率限制器 (设计)
随机推荐
<栈模拟递归>
数据系统分区设计 - 分区与二级索引
JVM knowledge brain map sharing
No tracked branch configured for branch xxx or the branch doesn‘t exist. To make your branch trac
Find out what happened in the process of new
PAT甲级1152 Google Recruitment (20 分)
LeetCode - 359 日志速率限制器 (设计)
IDEA—点击文件代码与目录自动同步对应
Understanding of this object
Pat grade a 1153 decode registration card of PAT (25 points)
Graph theory and concept
自定义注解校验API参数电话号
Flex layout
2019 Shaanxi provincial competition j-bit operation + greed
Flex 布局
Games101 review: Transformation
Endnote 无法编辑range 解决
Icpc2021 Kunming m-violence + chairman tree
GAMES101复习:三维变换
User defined annotation verification API parameter phone number