当前位置:网站首页>leetcode刷题:栈与队列01(用栈实现队列)
leetcode刷题:栈与队列01(用栈实现队列)
2022-07-01 19:16:00 【涛涛英语学不进去】
使用栈实现队列的下列操作:
push(x) – 将一个元素放入队列的尾部。
pop() – 从队列首部移除元素。
peek() – 返回队列首部的元素。
empty() – 返回队列是否为空。
示例:
MyQueue queue = new MyQueue();
queue.push(1);
queue.push(2);
queue.peek(); // 返回 1
queue.pop(); // 返回 1
queue.empty(); // 返回 false
说明:
- 你只能使用标准的栈操作 – 也就是只有 push to top, peek/pop from top, size, 和 is empty 操作是合法的。
- 你所使用的语言也许不支持栈。你可以使用 list 或者 deque(双端队列)来模拟一个栈,只要是标准的栈操作即可。
- 假设所有操作都是有效的 (例如,一个空的队列不会调用 pop 或者 peek 操作)。
思路:就是两个栈嘛,一个用来接数据,一个用来出数据, 先进 后出 后进 先出。
入栈就直接入数据的栈 接数据 push了。
出栈和查看栈差不多,先看看出数据的栈是否是空的,如果是空的,把入数据的栈全推到出数据的栈, 先进 +( 后出 + 后进 )+ 先出 = 先进 先出
判空就更好做了,看看两个栈如果都是空才算空。
package com.programmercarl.stacks_queues;
import java.util.Stack;
/** * @ClassName MyQueue * @Descriotion TODO * @Author nitaotao * @Date 2022/6/29 10:57 * @Version 1.0 * 232. 用栈实现队列 * https://leetcode.cn/problems/implement-queue-using-stacks/ * 思路: * 栈,先进后出 * 队列,先进先出 * 栈模拟队列,一个元素从第一个栈入,出来后再推入第二个栈,则 先进 后出 后进 先出 == 先进 先出 **/
public class MyQueue {
private Stack stackIn;
private Stack stackOut;
public MyQueue() {
stackIn = new Stack();
stackOut = new Stack();
}
/** * 推入一个元素 * * @param x */
public void push(int x) {
stackIn.push(x);
}
/** * 返回栈顶元素 * * @return */
public int pop() {
int lenIn = stackIn.size();
int lenOut = stackOut.size();
//老区先出完,新区再进
if (lenOut > 0) {
return (int) stackOut.pop();
} else {
//老区无元素,新区进老区
while (lenIn > 0) {
stackOut.push(stackIn.pop());
lenIn--;
}
return (int) stackOut.pop();
}
}
/** * 查看栈顶元素 * * @return */
public int peek() {
int lenIn = stackIn.size();
int lenOut = stackOut.size();
//老区先出完,新区再进
if (lenOut > 0) {
return (int) stackOut.peek();
} else {
//老区无元素,新区进老区
while (lenIn > 0) {
stackOut.push(stackIn.pop());
lenIn--;
}
return (int) stackOut.peek();
}
}
/** * 判空 * * @return */
public boolean empty() {
return stackIn.isEmpty() && stackOut.isEmpty();
}
}
/** * 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(); */

边栏推荐
- 天气预报小程序源码 天气类微信小程序源码
- Écrire un document de blog
- Use of common built-in classes of JS
- Detailed explanation and code example of affinity propagation clustering calculation formula based on graph
- Optimization of the problem that the request flow fails to terminate during page switching of easycvr cluster video Plaza
- C # joint Halcon application - Dahua camera acquisition class
- Data analysts sound tall? Understand these points before you decide whether to transform
- 朋友圈社区程序源码分享
- Keras machine translation practice
- What if the win11 shortcut key switching input method doesn't respond? Shortcut key switching input method does not respond
猜你喜欢

Gaussdb (for MySQL):partial result cache, which accelerates the operator by caching intermediate results

新版Free手机、PC、平板、笔记本四端网站缩略展示图在线一键生成网站源码

图片拼图微信小程序源码_支持多模板制作和流量主

GaussDB(for MySQL) :Partial Result Cache,通过缓存中间结果对算子进行加速

Entering Ruxin Town, digital intelligence transformation connects "future community"

强大的万年历微信小程序源码-支持多做流量主模式

Common components of flask

Exclusive news: Alibaba cloud quietly launched RPA cloud computer and has opened cooperation with many RPA manufacturers

Develop those things: easycvr cluster device management page function display optimization

3D panoramic model display visualization technology demonstration
随机推荐
强大、好用、适合程序员/软件开发者的专业编辑器/笔记软件综合评测和全面推荐
Using qeventloop to realize synchronous waiting for the return of slot function
C#聯合halcon應用——大華相機采集類
Internship: complex JSON format data compilation interface
8K HDR!|为 Chromium 实现 HEVC 硬解 - 原理/实测指南
朋友圈社区程序源码分享
#yyds干货盘点#SQL聚合查询方法总结
Test of NSI script
随机头像大全,多分类带历史记录微信小程序源码_支持流量主
【蓝桥杯Web】2022年第十三届蓝桥杯Web大学组国赛真题解析
tensorflow 张量做卷积,输入量与卷积核维度的理解
C # joint halcon Application - Dahua Camera Collection class
2022年低压电工考试试题及答案
2022/6/8-2022/6/12
深度学习 神经网络基础
Powerful, easy-to-use, professional editor / notebook software suitable for programmers / software developers, comprehensive evaluation and comprehensive recommendation
基于图的 Affinity Propagation 聚类计算公式详解和代码示例
math_ Use differentiation to calculate approximate value
【let var const】
Importance of EDA tools to chip industry knowledge popularization