Leetcode_252_Implement Stack using Queues
本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/48598773
Implement the following operations of a stack using queues.
- push(x) -- Push element x onto stack.
- pop() -- Removes the element on top of the stack.
- top() -- Get the top element.
- empty() -- Return whether the stack is empty.
Notes:
- You must use only standard operations of a queue -- which means only
push to back,peek/pop from front,size, andis emptyoperations are valid. - Depending on your language, queue may not be supported natively. You may simulate a queue by using a list or deque (double-ended queue), as long as you use only standard operations of a queue.
- You may assume that all operations are valid (for example, no pop or top operations will be called on an empty stack).
思路:
(1)题意为运用队列来实现栈,主要包括push()、pop()、top()、empty()方法的实现。
(2)该题的思路和“运用栈来实现队列”类似。首先还是得了解栈和队列的特性,这里再复习一遍;栈:先进后出,只能在栈顶添加和删除元素,队列:先进先出,只能在队尾添加元素,从队头移除元素。想用队列实现栈,主要需考虑到如何将先放入的元素先出栈;这里需要用两个队列来实现,其中一个队列在执行操作后为空,另一个队列存放元素。当有元素加入时,如果某一队列不为空,则将元素加入该队列中;当有元素出栈时,此时需从不为空的队列list中将除去队尾元素的其余元素全部加入另一个空的队列list0中,这样,list队列中的队尾元素(对应栈顶元素)就没有被加入到list0中,然后将list置为一个空的队列,即完成出栈操作。其余的操作比较简单,请参见代码。
(3)详情见下方代码。希望本文对你有所帮助。
算法代码实现如下:
package leetcode;
import java.util.LinkedList;
import java.util.List;
/**
*
* @author liqqc
*
*/
public class Implement_Stack_using_Queues {
// Push element x onto stack.
private List<Integer> list = new LinkedList<Integer>();
private List<Integer> list0 = new LinkedList<Integer>();
public void push(int x) {
if (list.size() == 0 && list0.size() == 0) {
list.add(x);
return;
}
if (list.size() != 0) {
list.add(x);
return;
}
if (list0.size() != 0) {
list0.add(x);
}
}
// Removes the element on top of the stack.
public void pop() {
if (list.size() != 0 && list0.size() == 0) {
for (int i = 0; i < list.size() - 1; i++) {
list0.add(list.get(i));
}
list.clear();
return;
}
if (list.size() == 0 && list0.size() != 0) {
for (int i = 0; i < list0.size() - 1; i++) {
list.add(list0.get(i));
}
list0.clear();
}
}
// Get the top element.
public int top() {
if (list.size() != 0 && list0.size() == 0) {
return list.get(list.size() - 1);
}
if (list.size() == 0 && list0.size() != 0) {
return list0.get(list0.size() - 1);
}
return -1;
}
// Return whether the stack is empty.
public boolean empty() {
return list0.size() == 0 && list.size() == 0;
}
}
Leetcode_252_Implement Stack using Queues的更多相关文章
- [LeetCode] Implement Stack using Queues 用队列来实现栈
Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...
- Java for LeetCode 225 Implement Stack using Queues
Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...
- Implement Stack using Queues
Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...
- (leetcode)Implement Stack using Queues
Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...
- (easy)LeetCode 225.Implement Stack using Queues
Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...
- leetcode:Implement Stack using Queues 与 Implement Queue using Stacks
一.Implement Stack using Queues Implement the following operations of a stack using queues. push(x) - ...
- 【LeetCode】232 & 225 - Implement Queue using Stacks & Implement Stack using Queues
232 - Implement Queue using Stacks Implement the following operations of a queue using stacks. push( ...
- Leetcode 225 Implement Stack using Queues
Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...
- Java [Leetcode 225]Implement Stack using Queues
题目描述: Implement the following operations of a stack using queues. push(x) -- Push element x onto sta ...
随机推荐
- Java进阶(四十七)Socket通信
Java进阶(四十七)Socket通信 今天讲解一个 Hello Word 级别的 Java Socket 通信的例子.具体通讯过程如下: 先启动Server端,进入一个死循环以便一直监听某端口是 ...
- EJB3基本概念、运行环境、下载安装与运行jboss
EJB3基本概念 什么是EJB: EJB(EnterpriceJavaBeans)是一个用于分布式业务应用的标准服务端组件模型.采用EJB架构编写的应用是可伸的.事务性的.多用户安全的.采用EJB编写 ...
- linux的 压缩与解压 命令集
bzip2压缩费时但效果好,而且支持hadoop的hdfs文件切分,gzip不行 bzip2 [-cdz] 文件名 -c :将压缩的过程输出到屏幕 -d :解压缩 -z :压缩 -# :压缩比的参数, ...
- EBS销售(OE)模块常用表
select * from ra_customers 客户 select * from ra_addresses_all 地址 select * from ra_site_uses_all 用户 ...
- memcached实战系列(四)memcached stats命令 memcached优化
memcached提供一系列的命令进行优化的查看,方便我们调整我们的存储策略,查看我们的使用率,内存的使用率以及浪费情况.常用的命令有stats.stats settings.stats items. ...
- 1.使用C++封装一个链表类LinkList
使用C++封装一个链表类LinkList.写出相应一个测试用例 链表需要提供 添加 修改删除 除重 合并 排序创建 销毁等接口. 不能调用库函数或者使用STL等类库 题目延伸********** ...
- Gradle 的Daemon配置
最近升级到Android 2.2.2之后,运行之前的项目特别卡,基本上2分钟,好的时候1分半,查询了Android官网的说明说daemon能够加快编译.于是我也尝试开启Daemon. 在Windows ...
- GCD API 理解 (一)
资料先行 GCD 深入理解:第一部分 GCD 深入理解:第二部分 以上两篇文章是关于GCD讲的比较好的文章,翻译自raywenderlich,该网站有很多关于iOS 开发的优秀文章. 引子 iOS 开 ...
- java基础---Java---面试题---银行业务调度系统(线程同步锁、枚举、线程池)
银行业务调度系统的项目需求: 模拟实现银行业务调度系统逻辑,具体需求如下: Ø 银行内有6个业务窗口,1- 4号窗口为普通窗口,5号窗口为快速窗口,6号窗口为VIP窗口. Ø 有三种对应类 ...
- 如何在Cocos2D 1.0 中掩饰一个精灵(一)
大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请告诉我,如果觉得不错请多多支持点赞.谢谢! hopy ;) 原帖来自Ray Wunderlich写的精彩的文章 How To ...