$用两个栈实现队列_Java版 @FDDLC
发布日期:2021-06-30 20:56:45 浏览次数:2 分类:技术文章

本文共 709 字,大约阅读时间需要 2 分钟。

题目描述

用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型。

 

AC代码(Java版)

import java.util.Stack;public class Solution {    Stack
stack1 = 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 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:#两个链表的第一个公共结点_Java版 @FDDLC
下一篇:#删除链表的倒数第n个节点_Java版 @FDDLC

发表评论

最新留言

表示我来过!
[***.240.166.169]2024年04月15日 12时43分56秒