当前位置:网站首页>[栈和队列简单题] 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();
}
}
边栏推荐
- Web3.0 world knowledge system sharing - what is Web3.0
- typora详细教程
- 调用JShaman的Web API接口,实现JS代码加密。
- Favicon网页收藏图标在线制作PHP网站源码/ICO图片在线生成/支持多种图片格式转换
- Is the low commission account opening of Galaxy Securities Fund reliable, reliable and safe
- coco test-dev 测试代码
- GoatGui邀你参加机器学习研讨班
- 【Redis】五种常用的数据类型
- 从ACL 2022 Onsite经历看NLP热点
- Interrupt, signal, system call
猜你喜欢

Plato Farm全新玩法,套利ePLATO稳获超高收益

Function stack frame explanation

「软件测试」包装简历从这几点出发,直接提升通过率

Pyqt5 use pyqtgraph to draw dynamic scatter chart

Lua函数之非全局函数

If you want to thoroughly optimize the performance, you must first understand the underlying logic~

Swiperjs custom width

Kubernetes dashboard deployment application and access

智能指针shared_ptr、unique_ptr、weak_ptr

C language program compilation
随机推荐
Cookie addition, deletion, modification and query methods
Arduino UNO +74hc164 water lamp example
C language: deep learning recursion
ArduinoUNO驱动RGB模块全彩效果示例
快速排序(Quick sort)
Cuteone: a onedrive multi network disk mounting program / with member / synchronization and other functions
平成千字文(へいせいせんじもん) (平成12年9月10日 石渡 明 作) 宇宙広遠 銀河永久 日月運行 不乱無休 地球公転 季節変移 黄道星座 太陽年周 故郷群島 南熱北冷 海洋温暖 気候順良 青空飛雲 諸野深緑 湖泉静息 谷川清流 春桜一面 新芽
Leetcode- > binary search clock in
Greed - 376. Swing sequence
Scheduling of processes
论构造函数的原型是谁
Use of formdata
ZJCTF_login
shell(38) : ssh端口转发
go实现导出excel表格
解决小程序报错getLocation:fail the api need to be declared in the requiredPrivateInfos field in app.json
阿里云解决方案架构师张平:云原生数字化安全生产的体系建设
Debezium series: the binlog file cannot be recovered after the record is hung from the library server, and the task is switched to the main library to ensure that the data is not lost
Database knowledge required by testers: MySQL common syntax
Web3.0 world knowledge system sharing - what is Web3.0