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 ...
随机推荐
- [django]用fastcgi部署
django官方已经开始弃用fastcgi来部署django应用了,作为以前使用过的用户,还是贴一个配置,用来做纪念吧.. 项目下 #! /bin/sh case "$@" in ...
- BeanUtils 读取数据
前两篇文章都是关于setProperty的,下面来说一个关于getProperty 的小案例.如下: MyClass.java package beanutils; public class MyCl ...
- FORM的静态提交
在form中进行保存时,如果使用commit_form的话会弹出信息提示"没有修改需要保存"或者"几条记录已保存"类似的字样,有时候不想被提示,可以使用A ...
- webStorm破解
B4A73YYJ-eyJsaWNlbnNlSWQiOiI0M0I0QTczWVlKIiwibGljZW5zZWVOYW1lIjoibGFuIHl1IiwiYXNzaWduZWVOYW1lIjoiIiw ...
- TCP连接建立系列 — 客户端发送SYN段
主要内容:客户端调用connect()时的TCP层实现. 内核版本:3.15.2 我的博客:http://blog.csdn.net/zhangskd connect的TCP层实现 SOCK_STRE ...
- XML Schema
XML Schema 是基于 XML 的 DTD 替代者. XML Schema 描述 XML 文档的结构. XML Schema 语言也称作 XMLSchema 定义(XML Schema Defi ...
- IT女孩特不烦恼---九月实习总结
对着岁月落笔,画出一场清风,那是最真的笑容 一溜烟的功夫,小编来实习Android已经四个月了,从刚开始的电商项目到现在的车段子项目,小编渐渐对这个曾经陌生的名字慢慢扭转变成熟悉的面孔,四个月的时间, ...
- BCD码与16进制互转算法
关于这类算法,以前的文章已经讲过类似的:BCD码转二进制 #include <stdio.h> // HEX转BCD //bcd_data(<0x255,>0) unsigne ...
- 最简单的基于FFmpeg的视频编码器-更新版(YUV编码为HEVC(H.265))
===================================================== 最简单的基于FFmpeg的视频编码器文章列表: 最简单的基于FFMPEG的视频编码器(YUV ...
- UNIX环境高级编程——线程限制