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 ...
随机推荐
- 使用 纯JQuery 进行 表单 验证
对于JavaScript而言,进行表单数据的验证可谓是很有必要的,而且一般我们都会在网页上先进行一下表单验证,然后服务器端再次进行验证,来确保用户提交数据的准确性.下面就来分享一个JQuery实现的表 ...
- 携程React Native实践
React Native(下文简称 RN)开源已经一年多时间,国内各大互联网公司都在使用,携程也在今年 5 月份投入资源开始引入,并推广给多个业务团队使用,本文将会分享我们遇到的一些问题以及我们的优化 ...
- 4.0、Android Studio配置你的构建
Android构建系统编译你的app资源和源码并且打包到APK中,你可以用来测试,部署,签名和发布.Android Studio使用Gradle,一个高级的构建套件,来自动化和管理构建进程,同时可以允 ...
- android插件开发机制
插件机制实质上就是由主体程序定义接口,然后由插件去实现这些接口,以达到功能模块化.Android系统是基于Linux内核的,其安全机制也继承了Linux的特性,再加上android framework ...
- android AlarmManager讲解
Android系统闹钟定时功能框架,总体来说就是用数据库存储定时数据,有一个状态管理器来统一管理这些定时状态的触发和更新.在Andriod系统中实现定时功能,最终还是要用到系统提供的AlarmMana ...
- Android官方推荐使用DialogFragment替换AlertDialog
DialogFragment是在Android3.0(API level 11)中引入的,它代替了已经不建议使用的AlertDialog. DialogFragment高效地封装和管理对话框的生命周期 ...
- Android简易实战教程--第五话《开发一键锁屏应用》
转载请注明出处:http://blog.csdn.net/qq_32059827/article/details/51860900 点击打开链接 Device Administration 对于这个应 ...
- Android简易实战教程--第二话《两种进度条》
点击按钮模拟进度条下载进度,"下载"完成进度条消失. 代码如下: xml: <?xml version="1.0" encoding="utf- ...
- JAVA之旅(三十)——打印流PrintWriter,合并流,切割文件并且合并,对象的序列化Serializable,管道流,RandomAccessFile,IO其他类,字符编码
JAVA之旅(三十)--打印流PrintWriter,合并流,切割文件并且合并,对象的序列化Serializable,管道流,RandomAccessFile,IO其他类,字符编码 三十篇了,又是一个 ...
- cocos2d-js(二)cocos2d-js的基本语法与类的简介
基本语法: 1.类的定义 一般类都是集成Scene或者Layer: var myLayer = cc.Layer.extend({类的内容}); 2类内的成员变量与方法: 2.1成员变量的声明: 变量 ...