当前位置:网站首页>JZ9 用两个栈实现队列
JZ9 用两个栈实现队列
2022-08-02 15:35:00 【syc596】
JZ9 用两个栈实现队列
用两个栈实现队列_牛客题霸_牛客网 (nowcoder.com)
NC76 用两个栈实现队列
用两个栈实现队列_牛客题霸_牛客网 (nowcoder.com)
//11
//模拟队列
import java.util.*;
public class Solution {
Stack<Integer> st1 = new Stack<>();
Stack<Integer> st2 = new Stack<>();
public void push(int node) {
st1.push(node);
}
public int pop() {
if(st2.isEmpty()){
while(st1.isEmpty()==false){
st2.push(st1.pop());
}
}
return st2.pop();
}
}
import java.util.*;
public class Solution {
Stack<Integer> st1 = new Stack<>();
Stack<Integer> st2 = new Stack<>();
public void push(int node) {
st1.push(node);
}
public int pop() {
//将第一个栈中内容弹出放入第二个栈中
while(st1.isEmpty()==false){
st2.push(st1.pop());
}
int ret=st2.pop();
再将第二个栈的元素放回第一个栈
while(st2.isEmpty()==false){
st1.push(st2.pop());
}
return ret;
}
}
边栏推荐
猜你喜欢
随机推荐
剑指Offer 49.丑数 动态规划
MongoDB 《三》复制集集群搭建实例
MySQL【数据类型】
WWW'22 推荐系统论文之多任务与对比学习篇
Qt | 关于QPalette的使用
Go-5-简单介绍fmt库
MPLS实验
SIGIR'22 推荐系统论文之序列推荐(长文)篇
CWE4.8: The 25 most damaging software security issues in 2022
AI智能剪辑,仅需2秒一键提取精彩片段
MySQL-1-环境部署
JZ27 二叉树的镜像
2.6 - 进程资源
JZ4 二维数组中的查找
DevOps开发工具对比
Mobius inversion study notes
.NET性能优化-使用SourceGenerator-Logger记录日志
tiup mirror sign
策略路由下发
【[USACO12MAR]Cows in a Skyscraper G】【状压DP && DFS】