$用两个栈实现队列_Java版 @FDDLC
发布日期:2021-06-30 20:56:45
浏览次数:2
分类:技术文章
本文共 709 字,大约阅读时间需要 2 分钟。
题目描述
用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型。
AC代码(Java版):
import java.util.Stack;public class Solution { Stackstack1 = new Stack (); Stack stack2 = new Stack (); public void push(int node) { stack1.push(node); } public int pop() { while(!stack1.isEmpty()) { stack2.push(stack1.pop()); } int answer = stack2.pop(); while(!stack2.isEmpty()) { stack1.push(stack2.pop()); } return answer; } public static void main(String[] args) { Solution solution = new Solution(); solution.push(1); //solution.push(2); System.out.println(solution.pop()); }}
转载地址:https://liuxingchang.blog.csdn.net/article/details/109094904 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
表示我来过!
[***.240.166.169]2024年04月15日 12时43分56秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
COMP7404 Machine Learing——ROC
2019-04-30
Python量子计算qiskit
2019-04-30
Python的多线程不是真的多线程(GIL全局解释器锁)
2019-04-30
Python手动读取MNIST数据集
2019-04-30
Python手动读取CIFAR-10数据集
2019-04-30
Pytorch(十一) —— 分布式(多GPU)训练
2019-04-30
Deeplab v3
2019-04-30
tensor/矩阵/图片等更换通道,调整size
2019-04-30
本地和colab 中 改变tensorflow的版本
2019-04-30
CUB-200鸟类数据集
2019-04-30
Python反射机制
2019-04-30
YAPF —— Python代码格式化工具
2019-04-30
UGC 用户产生内容
2019-04-30
ranger
2019-04-30
slurm
2019-04-30
xfce4
2019-04-30
xrdp
2019-04-30
Raft算法
2019-04-30