当前位置:网站首页>[栈和队列简单题] LeetCode 232. 用栈实现队列,225. 用队列实现栈
[栈和队列简单题] LeetCode 232. 用栈实现队列,225. 用队列实现栈
2022-07-27 00:21:00 【哇咔咔负负得正】
LeetCode 232. 用栈实现队列
https://leetcode.cn/problems/implement-queue-using-stacks
请你仅使用两个栈实现先入先出队列。队列应当支持一般队列支持的所有操作(push、pop、peek、empty):
实现 MyQueue 类:
void push(int x) 将元素 x 推到队列的末尾int pop() 从队列的开头移除并返回元素int peek() 返回队列开头的元素boolean empty() 如果队列为空,返回 true ;否则,返回 false
前提条件:假设操作均合法。
Solution
出队时间复杂度为 O ( n ) O(n) O(n), 入队为 O ( 1 ) O(1) O(1)。
stack1 用于加入数据,stack2 用于取数据。
当取数据时若 stack2 为空,则从 stack1 依次取出数据放入 stack2。
class MyQueue {
private Stack<Integer> stack1 = new Stack<Integer>();
private Stack<Integer> stack2 = new Stack<Integer>();
public MyQueue() {
}
public void push(int x) {
stack1.push(x);
}
public int pop() {
if (stack2.empty()) {
while (!stack1.empty()) {
stack2.push(stack1.pop());
}
}
return stack2.pop();
}
public int peek() {
if (stack2.empty()) {
while (!stack1.empty()) {
stack2.push(stack1.pop());
}
}
return stack2.peek();
}
public boolean empty() {
return stack1.empty() && stack2.empty();
}
}
LeetCode 225. 用队列实现栈
https://leetcode.cn/problems/implement-stack-using-queues
请你仅使用两个队列实现一个后入先出(LIFO)的栈,并支持普通栈的全部四种操作(push、top、pop 和 empty)。
实现 MyStack 类:
void push(int x) 将元素 x 压入栈顶。int pop() 移除并返回栈顶元素。int top() 返回栈顶元素。boolean empty() 如果栈是空的,返回 true ;否则,返回 false 。
Solution
出栈时间复杂度为 O ( 1 ) O(1) O(1), 入栈为 O ( n ) O(n) O(n)。
入栈操作实现:先入 queue1,然后将 queue2 的数据依次倒入 queue1,然后交换 queue1 和 queue2,交换后 queue1 为空。
出栈操作和取栈顶元素操作实现:直接取 queue2 的队头即可。
判空实现:queue2 是否为空。
队列判空用 isEmpty()。
class MyStack {
private Queue<Integer> queue1 = new LinkedList<Integer>();
private Queue<Integer> queue2 = new LinkedList<Integer>();
public MyStack() {
}
public void push(int x) {
queue1.offer(x); // 有些队列有大小限制,若队满,调用 add() 方法抛出异常, 而 offer() 返回 false
// 将 queue2 的数据倒入 queue1
while (!queue2.isEmpty()) {
queue1.offer(queue2.poll());
}
Queue temp = queue1;
queue1 = queue2;
queue2 = temp;
}
public int pop() {
return queue2.poll(); // poll() 方法在用空集合调用时不是抛出异常,只是返回 null, remove() 方法会报异常
}
public int top() {
return queue2.peek(); // 在队列为空时, element() 抛出一个异常,而 peek() 返回 null
}
public boolean empty() {
return queue2.isEmpty();
}
}
边栏推荐
- 面试突击68:为什么 TCP 需要 3 次握手?
- Is the low commission account opening of Galaxy Securities Fund reliable, reliable and safe
- 次轮Okaleido Tiger即将登录Binance NFT,引发社区热议
- CS224W fall 1.2 Applications of Graph ML
- Comprehensive summary of shell analysis log file commands
- Kubernetes Dashboard 部署应用以及访问
- Jmeter接口测试, 快速完成一个单接口请求
- coco test-dev 测试代码
- Kubeadmin到底做了什么?
- Mysql 5.7 取分组第一条
猜你喜欢

Okaleido tiger is about to log in to binance NFT in the second round, which has aroused heated discussion in the community

函数栈帧详解

GoatGui邀你参加机器学习研讨班

Ten thousand words long text, take you to understand the kubernetes network model

Kubernetes dashboard deployment application and access

基于.NetCore开发博客项目 StarBlog - (16) 一些新功能 (监控/统计/配置/初始化)

Leetcode skimming -- no.238 -- product of arrays other than itself

time模块: 时间戳、结构化时间、格式化时间的获取与相互转化

Goatgui invites you to attend a machine learning seminar

Cuteone: a onedrive multi network disk mounting program / with member / synchronization and other functions
随机推荐
Web3.0世界知识体系分享-什么是Web3.0
Turn: YuMinHong: what hinders your growth is yourself
Kubernetes dashboard deployment application and access
idea中常用的快捷键
Why do people like to rank things
论构造函数的原型是谁
Interview shock 68: why does TCP need three handshakes?
贪心——376. 摆动序列
Cloud development sleeping alarm clock wechat applet source code
Cs224w fall course - --- 1.1 why graphs?
白盒测试案例设计(我爷爷都能看懂)
How small programs help the new model of smart home ecology
Okaleido Tiger 7.27日登录Binance NFT,首轮已获不俗成绩
Cuteone: a onedrive multi network disk mounting program / with member / synchronization and other functions
B-树的应用以及添加和删除操作
BP plug-in temporary code record
typora详细教程
杀毒软件 clamav 的安装和使用
数据库读写分离和分库分表
Kubernetes Dashboard 部署应用以及访问