两个队列实现一个栈,剑指offer P59
public class StackByQueue {
private LinkedList<String> queue1;
private LinkedList<String> queue2;
public StackByQueue() {
queue1 = new LinkedList<String>();
queue2 = new LinkedList<String>();
}
public String pop() {
String str = null;
if(queue1.size() ==0 && queue2.size() ==0) {
return null;
}
if(queue2.size() == 0) {
while(queue1.size() > 0) {
str = queue1.removeFirst();
if(queue1.size() != 0) {//要删除的栈元素不会加入队列的尾部
queue2.addLast(str);
}
}
}else if(queue1.size() == 0){
while(queue2.size() > 0) {
str = queue2.removeFirst();
if(queue2.size() != 0) {
queue1.addLast(str);
}
}
}
return str;
}
public void push(String str) {
if(queue1.size() == 0 && queue2.size() == 0) {
queue1.addLast(str);
}
if(queue1.size() != 0) {
queue1.addLast(str);
}else if (queue2.size() != 0) {
queue2.addLast(str);
}
}
public static void main(String[] args) {
StackByQueue stackByQueue = new StackByQueue();
stackByQueue.push("1");
stackByQueue.push("2");
stackByQueue.push("3");
System.out.println(stackByQueue.pop());
stackByQueue.push("8");
System.out.println(stackByQueue.pop());
}
}
两个队列实现一个栈,剑指offer P59的更多相关文章
- 《剑指Offer》附加题_用两个队列实现一个栈_C++版
在<剑指Offer>中,在栈和队列习题中,作者留下来一道题目供读者自己实现,即"用两个队列实现一个栈". 在计算机数据结构中,栈的特点是后进先出,即最后被压入(push ...
- 剑指offer编程题Java实现——面试题7相关题用两个队列实现一个栈
剑指offer面试题7相关题目:用两个队列实现一个栈 解题思路:根据栈的先入后出和队列的先入先出的特点1.在push的时候,把元素向非空的队列内添加2.在pop的时候,把不为空的队列中的size()- ...
- 两个栈实现队列 牛客网 剑指Offer
两个栈实现队列 牛客网 剑指Offer 题目描述 用两个栈来实现一个队列,完成队列的Push和Pop操作. 队列中的元素为int类型. class Solution: def __init__(sel ...
- C++两个队列实现一个栈
C++两个队列实现一个栈 /* * source.cpp * * Created on: 2015年6月21日 * Author: codekiller */ #include "iostr ...
- java两个栈实现一个队列&&两个队列实现一个栈
栈:先进后出 队列:先进先出 两个栈实现一个队列: 思路:先将数据存到第一个栈里,再将第一个栈里的元素全部出栈到第二个栈,第二个栈出栈,即可达到先进先出 源码: class Queue<E&g ...
- python两个队列实现一个栈和两个栈实现一个队列
1.两个栈实现一个队列 两个栈stack1和stack2, push的时候直接push进stack1,pop时需要判断stack1和stack2中的情况.如果stack2不为空的话,直接从stack2 ...
- 【校招面试 之 剑指offer】第9-2题 用两个队列实现一个栈
#include<iostream> #include<queue> using namespace std; // 对于出栈解决的思路是:将queue1的元素除了最后一个外全 ...
- 用两个栈实现队列,剑指offer P59
public class QueueByStack { private Stack<Integer> stack1; private Stack<Integer> stack2 ...
- C++版 - 剑指offer 面试题7:用两个栈实现队列 题解
用两个栈实现队列 提交网址: http://www.nowcoder.com/practice/54275ddae22f475981afa2244dd448c6?tpId=13&tqId=1 ...
随机推荐
- MS Sql Server 消除重复行 保留信息完整的一条 2011-11-26 13:19(QQ空间)
select company ,count(company) as coun into myls from mylist group by company having count(company)& ...
- 移动收入超PC端 盛大文学战略转型初见成效
随着智能手机和平板电脑的普及,越来越多的互联网服务也开始向移动端拓展,除了传统的互联网服务如搜索.即时通信之外,网络文学这项新兴的互联网业务也没忽视对移动端的布局. 7月9日,中国最大的网络文学出版平 ...
- 【.net 深呼吸】细说CodeDom(9):动态编译
知道了如果构建代码文档,知道了如何生成代码,那么编译程序集就很简单了. CodeDomProvider 类提供了三个可以执行编译的方法: 1.CompileAssemblyFromSource——这个 ...
- 计算字符串中每种字符出现的次数[Dictionary<char,int>泛型集合用法]
有一道经典的面试题: 统计 welcome to china中每个字符出现的次数,不考虑大小写. 第一个出现在脑海里的想法是: 1. 将字字符串转换成 char数组: 2. 用 for循环遍 ...
- USACO 4.2 The Perfect Stall(二分图匹配匈牙利算法)
The Perfect StallHal Burch Farmer John completed his new barn just last week, complete with all the ...
- FragmentPagerAdapter和FragmentStatePagerAdapter区别?
FragmentPagerAdapter:对于不再需要的fragment,选择调用detach方法,仅销毁视图,并不会销毁fragment实例.FragmentStatePagerAdapter:会销 ...
- angular1.x 脏检测
写在前面 双向绑定是angular的大亮点,然后支撑它的就是脏检测.一直对脏检测都有一些理解,却没有比较系统的概念. 以下是我阅读网上博文以及angular高级程序设计的理解与总结. 接收指导与批评. ...
- 7 -- Spring的基本用法 -- 10...
7.10 高级依赖关系配置 组件与组件之间的耦合,采用依赖注入管理:但基本类型的成员变量值,应直接在代码中设置. Spring支持将任意方法的返回值.类或对象的Field值.其他Bean的getter ...
- GourdScan & sqlmapapi
0x01 Windows下配置GourdScan 0x0101 GourdScan项目地址:https://github.com/code-scan/GourdScan PHP环境 + ...
- 随机love'...
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...